Skip Navigation

[Resolved] Hide custom posts with certain author role

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
- 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 1 reply, has 2 voices.

Last updated by Waqar 7 months, 1 week ago.

Assisted by: Waqar.

Author
Posts
#2694142

I have this site, where users can buy subscriptions and when active subscription they earn certain role, say "pro". If not active the are "dropped" to lower role, "out". The "pro" users can create posts and the posts are shown to the visitors in the site.

I know how to allow the post form to only those "pro" users.
But how can I hide the "out" users' posts? They may have been "pro"s before and created some posts then.

I can think of 2 solutions:
1. View filtered by post author's role
2. "out" users posts are turned into draft

I can think, but I can't make 🙂 - so would you please help me achieve either of those or perhaps you even have a cleverer solution. Thank you.

#2694382

Hi,

Thank you for contacting us and I'd be happy to assist.

Based on what you've shared, it would make more sense to exclude the posts, based on the author's user role.

You can use the 'get_users' function to get the list of users, belonging to a specific role:
https://developer.wordpress.org/reference/functions/get_users/

Then you can use the 'author__not_in' parameter in the query to exclude the posts from those users:
https://developer.wordpress.org/reference/classes/wp_query/#author-parameters

For views, you can use the 'wpv_filter_query' to attach function that does this filtering:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Example:


add_filter( 'wpv_filter_query', 'custom_filter_query_func', 1000 , 3 );
function custom_filter_query_func( $query_args, $view_settings ) {
	// process if specific view
	if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {
		// get list of IDs of users belonging to a specific user role
		$user_ids = get_users( [
			'role'   => 'role-name',
			'fields' => 'ID'
		] );
		// set posts from those users to be excluded
		$query_args['author__not_in'] = $user_ids; 
	}
	return $query_args;
}

Note: You'll replace '12345' with the target view's ID and 'role-name' with the target user role's slug.

For filtering these posts in archives, you can use the 'pre_get_posts' hook:
https://developer.wordpress.org/reference/hooks/pre_get_posts/

An example of such a function is available in this support ticket:
https://toolset.com/forums/topic/query-filter-for-user-role/#post-1218899

I hope this helps and please let me know if you need further assistance.

regards,
Waqar