Skip Navigation

[Resolved] Create a view that shows posts authored only by users with a specific role

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

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by sarahK-2 3 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#2115885

I would like to create a view that shows only Posts authored by Users with a specific Role (or set of roles).

I have a custom Role "Active Member", for example. The User has that role contingent on their current subscription membership via Memberpress. Only the posts of current subscription holders should be display in the view, and so I would like to filter by Role.

I have read (and tried following) the documentation here:
https://toolset.com/forums/topic/how-to-filter-a-view-by-specific-user-role/

I am able to get it to work very simply, and to display a couple of fields from the Post for each User with that Role. But the view ultimately needs to be ordered and sortable by Custom Post fields, not by attributes of the users. Totally lost! Is there any way to do this? Can I have a robust, searchable, sorted view of a custom post type that is filtered by Role of the User/author? (And if so, how?)

#2116151

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

If you want a view to display the results sort by custom post field then you will have to create a post type view.

To filter the posts based on specific role, you will have to use the view's hook "wpv_filter_query" where you should first try to find all the users belongs to specific role - for example:

$args1 = array(
'fields'=>'ID',
 'role' => 'subscriber',
 'orderby' => 'user_nicename',
 'order' => 'ASC'
);
 $subscribers = get_users($args1);

The above code should return you all the User IDs belongs to 'subscriber' user role and then you should hook in the found user IDs.

function func_filter_view_basedon_role( $query_args ,$view_settings, $view_id ) {
    global $post;
     
    if ( $view_id == 99999) {
       
$args1 = array( 'fields'=>'ID',
                                'role' => 'subscriber',
                                'orderby' => 'user_nicename',
                                 'order' => 'ASC'
                            );
 $found_user_ids = get_users($args1);

$query_args['author__in '] = (!empty($found_user_ids))? $found_user_ids:array(0);


      
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'func_filter_view_basedon_role', 10, 3);

Where:
- Replace 99999 with your original view Id.
- You should adjust the above code as per your custom role

More info:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#2120319

My issue is resolved now. Thank you!