1

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

1 Answer 1

2

This code will do the job...

<?php
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.

6
  • Yes, it's working if you add the get_currentuserinfo() before the "if" statement ;) Mar 2, 2012 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 ;) Mar 2, 2012 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. Mar 3, 2012 at 13:07
  • Something must've gone wrong in the condition probably... can you create a paste on pastebin of your code? Mar 3, 2012 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, 2013 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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