Skip Navigation

[Resolved] Limit View output to one post per author before search / filtering is applied

This support ticket is created 3 years, 7 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 4 replies, has 3 voices.

Last updated by andrei-laurentiuP 3 years, 7 months ago.

Assisted by: Waqar.

Author
Posts
#1763143

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/

#1763157

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/

#1763315

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.

#1764307

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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

#1765509

It works perfectly. Thank you very much for your support!! My issue is resolved now. Thank you!

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