Exclude posts from a View by authors with a specified role

A View lists posts, but you want to exclude posts by authors that have a given role.

You can add the following code (to Toolset > Settings > Custom Code), editing the View ID as well as the role to be excluded:

/**
 * Exclude posts by authors with a given role.
 */
function tssupp_filter_query($view_args, $view_settings, $view_id) {
 
    if (in_array($view_id, array(54))) { // Edit View ID
 
        // get all users with specific role
        $unwanted_users = get_users(array('role__in' => array('contributor'))); // Edit role
        $unwanted_user_ids = wp_list_pluck($unwanted_users, 'ID');
 
        $view_args['author__not_in'] = $unwanted_user_ids;
    }
 
    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);

Let us know if this snippet is not working for you:

This snippet doesn’t work
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments