Skip Navigation

[Resolved] Custom archive query

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)

Author
Posts
#2520445

Tell us what you are trying to do?
I'm implementing a site with both "directory" and "membership" features. It is a directory of businesses owned by members of an association of renovating businesses. I have the site essentially complete except for one feature: only displaying the business listings of "active" members. I have created two custom user roles to allow for this: "Active Member" and "Inactive Member". The missing piece is a custom query for the business CPT archive page to display only businesses that have an author whose user role is "Active Member".

Is there any documentation that you are following?
I've been (generally) following the "directory" and "membership" use cases for Toolset.

Is there a similar example that we can see?
I'm sure there is, but I'm not aware of any specific cases...

What is the link to your site?
nariindy.cardinalacres.com

#2521689

Hi,

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

To achieve this, you can customize the query for the specific post-type archive, using the "pre_get_posts" filter:
https://developer.wordpress.org/reference/hooks/pre_get_posts/

For example, if you'd like to target the post type archive for the post type with slug 'book' so that only the posts from the user role 'administrator' are shown, the code will look like this:


function target_specific_post_type_archive( $query ) {
	if ( ! is_admin() && $query->is_main_query() ) {
		// Not a query for an admin page.
		// It's the main query for a front-end page of your site.

		// check if it is a specific post type archive
		if ( is_post_type_archive( 'book' )  ) {
			// get all users with the specific role
			$user_ids = get_users( [
				'role'   => 'administrator',
				'fields' => 'ID'
			] );
			
			// include posts from only the users from that specific role
			$query->set( 'author__in', $user_ids );
		}
	}
}
add_action( 'pre_get_posts', 'target_specific_post_type_archive' );

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2522235

This works perfectly. Only issue is that if there are a large number of site users, the query to find all users with a given role may take some time to fetch. I suspect that a caching plugin would resolve the issue if there isn't a more efficient way to make this query directly in WP.

Thank you!

#2522779

So, I just realized that I also have a "featured business" function defined with a Toolset "View" that returns/displays a random business on the home page of this site. Obviously, I need to pre-filter the set of potential Business CPTs for the view to those authored by a user with role "Active Member".

It appears that this is doable using the wpv_filter_query in the same way that the WordPress archive query can be modified by pre_get_posts. Is that correct? Is the syntax for modifying the view query parameters the same (obviously, the checks for admin page and front-end queries necessary for modifying the WordPress archive query aren't necessary here as we can modify a specify Toolset view).

Thanks again for the extremely informative help...

#2526219

Thank you for waiting as we were a little light on the coverage, due to the holidays.

Your understanding is correct and for customizing the view's query, the "wpv_filter_query" filter can be used and the base structure of the query will remain the same:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For example, if the target view's ID is 12345:


add_filter( 'wpv_filter_query', 'custom_wpv_filter_query_func', 1000 , 3 );
function custom_wpv_filter_query_func( $query_args, $view_settings ) {

	// check if its the specific view
	if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {

		$user_ids = get_users( [
			'role'   => 'administrator',
			'fields' => 'ID'
		] );

		$query_args['author__in'] = $user_ids;

	}

	return $query_args;

}

#2526459

Works perfectly. Thank you!