Filter a list by user role

Hello all! So glad I found this forum. I would appreciate some assistance please.

I’m working on a filter for a Custom Post Type . I need it to filter the list depending on the user’s role. The way this should work is the following…

  • Users in roles “formusers1” and “formusers2” can post. Users can only see their own posts.
  • Users in role “formchecker1” can see all posts assigned role "formusers1’ and can approve each post.
  • Users in role “formchecker2” can see all posts assigned role "formusers2’ and can approve each post.
  • Users in roles “formsupervisor” and “administrator” can see everyone’s posts.

So far I can filter by roles “formusers1” and “formusers2” using $query->set('author', $current_user->ID); . However, when try to filter the list for role “formchecker1” I see posts from all roles. What am I doing wrong? Here’s the rest of the code. Thanks for checking out!

add_action('pre_get_posts', 'filter_posts_list');

function filter_posts_list($query) {
    //GLOBAL VARIABLES
    global $pagenow, $typenow;

    //MY VARIABLES
    global $current_user;
    get_currentuserinfo();

    //FILTERING
    if (current_user_can('formchecker1') && ('edit.php' == $pagenow) &&  $typenow == 'mycustomcpt' ) {
        $query->set('author', 'formusers1');
    }

    if (current_user_can('formchecker2') && ('edit.php' == $pagenow) &&  $typenow == 'mycustomcpt' ) {
        $query->set('author', 'formusers2');
    }

    if ((current_user_can('formusers1') || current_user_can('formusers2')) && ('edit.php' == $pagenow) &&  $typenow == 'mycustomcpt') {
        $query->set('author', $current_user->ID);
    }
}
Sponsor our Newsletter | Privacy Policy | Terms of Service