Jump to content

Filtering a Wordpress admin post list by role


Tectonic

Recommended Posts

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);
    }
}
```

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.