Skip Navigation

[Resolved] Creating a View that Displays Author Posts Based On Role

This support ticket is created 4 years, 1 month 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
- 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 4 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#1541259

Tell us what you are trying to do?

Currently, I have a plugin that allows users to submit a resume post, but there is no way to filter the posts by the user role. I would like to create a view that displays posts of all authors of a certain role. For example, I would like to be able to display posts of all users with the role "french student" on a given page, but on another page display all posts of users with the role "science student". Is it possible to display the posts of users of a specific role?

Is there any documentation that you are following?
https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/

What is the link to your site?
hidden link

#1541975

Waqar
Supporter

Languages: English (English )

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

Hi Michael,

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

To achieve this, you'll need two separate views:

1. First, you'll create a user view, which will give you the list of all the users of your desired role.

In the "Content Selection" setting of this view, you'll select "Any role", as you'll be filtering the role through the "wpv_filter_user_query" hook:
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_user_query )


add_filter( 'wpv_filter_user_query', 'filter_products_view_test', 1000 , 3 );
function filter_products_view_test( $query_args, $view_settings ) {
	// check if it is the target view
	if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12) ) {
		// get the value of the role passed through the shortcode attribute 
		global $WP_Views;
		$attributes = $WP_Views->view_shortcode_attributes;

		// if no role is set from the view's settings and role value is passed through the shortcode attribute
		if( (empty($query_args['role__in'])) && (!empty($attributes[0]['role'])) ) {
			// set query to only include users from the role passed through the shortcode attribute
			$query_args['role__in'][0] = $attributes[0]['role'];
		}
	}

	return $query_args;
}

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 active theme's "functions.php" file.

It will get the role's slug from the view's shortcode attribute and will filter the view's list to only return users with that role.

Example usage of the view's shortcode, assuming the slug of this new view is "user-view-slug":
(you'll need to change this slug with the one used on your website)

To get only the list of users with the "administrator" role:


[wpv-view name="user-view-slug" role="administrator"]

To get only the list of users with the "editor" role:


[wpv-view name="user-view-slug" role="editor"]

And so on.

2. The second view will be a post view, that will show the actual resume posts.

In the "Query Filter" section you'll include a post author filter with "Post author is set by the parent User View" option.

Example screenshot: hidden link

As a result, when you'll nest this post view inside the loop of the parent user view, you'll get all the posts from each user of the target role.

I hope this helps and let me know if any step is not clear.

Note: The code snippet included in this reply is shared as an example and for more personalized assistance around custom code, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

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