I am trying to set a cpt view to display only one post from each author before any search / filtering is applied.
There is a function that seems to apply such feature, but runs everywhere. I would like to run something similar only for one particular view, but I can't get the if statement to work properly, nor to change add_filter to something to return $content with wpv_filter_query...
Here is the basic start:
function filter_authors($groupby) {
global $wpdb;
$groupby = " {$wpdb->posts}.post_author";
return $groupby;
}
add_filter('posts_groupby', 'filter_authors');
Taken from:
https://toolset.com/forums/topic/limit-view-output-to-one-post-per-author/
Hello,
You can try trigger your custom PHP codes with wpv_filter_query filter hook, then remove it with function remove_filter(), for example:
add_filter( 'wpv_filter_query', 'prefix_show_only_author', 99, 3 );
function prefix_show_only_author( $query_args, $settings, $view_id ) {
if($view_id == 123){ // here replace 123 with your view's ID
add_filter('posts_groupby', 'filter_authors');
}
return $query_args;
}
function filter_authors($groupby) {
global $wpdb;
$groupby = " {$wpdb->posts}.post_author";
remove_filter('posts_groupby', 'filter_authors');
return $groupby;
}
Please replace 123 with your view's ID
More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://developer.wordpress.org/reference/functions/remove_filter/
Thank you!!! I am also trying to remove the grouping (by author) after filtering is initiated for the cpt but I don't manage to get it working. When I hit the text search, the grouping stops, but it does not happen when I filter by custom post type (e.g. price range). The view is set with individual settings: Update the Views results without reloading the page / Update URLs after loading search results (if relevant).
I tried adding:
if ( !isset( $query_args->query['meta_query'] ) && !isset( $query_args->query['tax_query'] ) && !isset( $query_args->query['s'] ) ) {
remove_filter('posts_groupby', 'filter_authors');
}
else
{
add_filter('posts_groupby', 'filter_authors');
}
But it doesn't work...
Any idea on how I should approach this?
Thank you again for your kindness & support,
A.
Hi,
Thanks for writing back.
Luo is on a holiday today, so I'll be following up on this ticket.
In my tests, this code works to stop the effect of grouping after any search filter has been used:
add_filter( 'wpv_filter_query', 'prefix_show_only_author', 99, 3 );
function prefix_show_only_author( $query_args, $settings, $view_id ) {
if($view_id == 123){ // here replace 123 with your view's ID
if( !isset($_GET['wpv_view_count']) ) {
add_filter('posts_groupby', 'filter_authors');
}
}
return $query_args;
}
function filter_authors($groupby) {
global $wpdb;
$groupby = " {$wpdb->posts}.post_author";
remove_filter('posts_groupby', 'filter_authors');
return $groupby;
}
I hope this helps and please let me know how it goes.
regards,
Waqar
It works perfectly. Thank you very much for your support!! My issue is resolved now. Thank you!