In the backend, how can I restrict the editors (user with editor role) from viewing/editing pages created by admin (and/or by administrator role)?

I know that the editor role in WordPress have "edit_others_pages" capability, but I need a function for a custom capability like: "edit_others_pages_except_admin" :)

I don't want to use a huge/complicated plugin just for that!

Thanks! I searched for a solution more then 1h :D


This code will do the job...

add_action('pre_get_posts', 'filter_posts_list');
function filter_posts_list($query)
    //$pagenow holds the name of the current page being viewed, we want to run our code only on edit.php (posts list)
    global $pagenow;

    //If the 'Editor' is logged in, exclude 'Admin's posts
    if(current_user_can('editor') && ('edit.php' == $pagenow))
        //global $query's set() method for excluding admin's posts
        $query->set('author', '-1');

For a detailed explanation Read Here.

| improve this answer | |
  • Yes, it's working if you add the get_currentuserinfo() before the "if" statement ;) – Nicusor Dumbrava Mar 2 '12 at 6:33
  • I thought get_currentuserinfo() won't be needed as we're using current_user_can()! Glad it helped you! Please accept the answer ;) – Rutwick Gangurde Mar 2 '12 at 6:37
  • Strange thing happen... today it doesn't work anymore. I'm logged with admin and I don't see my pages (Author:Admin). I also delete the get_currentuserinfo() lines... but still no pages, I see only the pages created by editors. – Nicusor Dumbrava Mar 3 '12 at 13:07
  • Something must've gone wrong in the condition probably... can you create a paste on pastebin of your code? – Rutwick Gangurde Mar 3 '12 at 13:32
  • 1
    The code works for me, but it would be advisable to use a capability instead of a role, as this is not guaranteed to work correctly. – brasofilo Jan 9 '13 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.