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.
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);
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: