Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

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.

share|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
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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