Skip Navigation

[Resolved] Filter View based on User Role

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to Filter View based on User Role

Solution:
To filter posts based on the role, you will require to use the Toolset Views/Blocks hook "wpv_filter_query" to filter the view/block query on fly.

You can find proposed solution in this case with the following reply:
https://toolset.com/forums/topic/filter-view-based-on-user-role/#post-2330683

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 2 years, 6 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
- 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 Paul 2 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2330657

Hi there,

Please can you help. I need to filter a View based on a user role. I.e. Teacher.

I've found the code below, but the Meta_Key is wrong. Can you help? What should I change that segment to so that I can look for only posts related to a role. I can't find the right meta_key to use...

//filter based on user roles
function func_filter_view_basedon_role( $query_args ,$view_settings, $view_id ) {
    global $WP_Views;
      
    if ( $view_id == 15528) {
      
     $args1 = array( 'fields'=>'ids',
                                'role' => $WP_Views->view_shortcode_attributes[0]['role'],
                                'orderby' => 'user_nicename',
                                'order' => 'ASC'
                            );
      $found_user_ids = get_users($args1);
      if(empty($found_user_ids)){
         $found_user_ids = array(-1);
      }
       
     $query_args['meta_key'] = 'role';
     $query_args['meta_value'] =  $found_user_ids;
       
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'func_filter_view_basedon_role', 99, 3);
#2330683

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I already shared the solution with the following link how you can filter the user posts based on the specific role:
=> https://toolset.com/forums/topic/create-a-view-which-displays-only-posts-created-by-users-with-the-role-author/#post-2255909

It seems based on the code you shared, you are passing the role name using the view's shortcode attribute, if that's correct then can you please try to use the following code:

function func_filter_view_basedon_role( $query_args ,$view_settings, $view_id ) {
    global $WP_Views;
       
    if ( $view_id == 15528) {
       
     $args1 = array( 'fields'=>'ID',
                                'role' => $WP_Views->view_shortcode_attributes[0]['role'],
                                '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', 99, 3);
#2330699

Thanks so much my friend for awesome support. Have a nice day 🙂

My issue is resolved now. Thank you!

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