Skip Navigation

[Resolved] Use the Views API on WordPress Archives

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to use the Views API on WordPress Archives?

Solution:
WordPress archives do not support the Views API.
Only

pre_get_posts

will work on archives.

You can, however, add queries to the Archive by setting them in the Toolset WordPress archive, if you enable the full custom view (Screen options of the archive)
There, you can add queries by fields, terms, and more.

This support ticket is created 5 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 2 replies, has 2 voices.

Last updated by mihailC 5 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#1201243

I try to modify the Query in custom post type archives created in Toolset->Wordpress Archives with "wpv_filter_query" filter hook. It is possible? This code didn't work (it's nothing changing on page):

add_filter( 'wpv_filter_query', 'modify_text_testimonials_query', 101, 3 );
function modify_text_testimonials_query( $query_args, $view_settings, $view_id ) {
if( is_post_type_archive( 'text-testimonials' ) ){
$query_args['post__in'] = my_custom_array(1, 2, 3);
$query_args['orderby'] = 'post__in';

}
return $query_args;
}

I was try to modify code like Minesh wrote here: https://toolset.com/forums/topic/display-only-the-latest-post-with-duplicated-taxonomy/
But this code didn't work too (it's nothing changing on page).

Basically, i prepare array with post ID's, ordered by current site language (WPML). And after, i want to replace the $query_args['post__in'] with that array. How to do that with "View Archives" feature?

Here is my code to prepare array with ID's:

function sort_text_testimonials_by_language() {

$post_ids = array();
$current_page_language = apply_filters( 'wpml_current_language', NULL );

$current_page_language_post_ids = get_posts( [
'post_type' => 'text-testimonials',
'post_status' => 'publish',
'meta_key' => 'wpcf-original-language',
'meta_value' => $current_page_language,
'posts_per_page' => -1,
'fields' => 'ids',
'order' => "DESC",
'orderby' => 'date'
] );

$all_post_ids = get_posts( [
'post_type' => 'text-testimonials',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'wpcf-original-language',
'value' => $current_page_language,
'compare' => '!=',
'type' => 'CHAR'
)
),
'fields' => 'ids',
'order' => "DESC",
'orderby' => 'date'
] );

$post_ids_merged = array_merge( $current_page_language_post_ids, $all_post_ids );
$post_ids = array_unique( $post_ids_merged );

return $post_ids;

}

#1201258

WordPress archives do not support the Views API.
Only pre_get_posts will work on archives.

You can, however, add queries to the Archive by setting them in the Toolset WordPress archive, if you enable the full custom view (Screen options of the archive)
There, you can add queries by fields, terms, and more.

#1201286

My issue is resolved now. Thank you!

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.