How to restrict the editors from viewing/editing pages created by admin?

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” 🙂

Read More

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

Thanks! I searched for a solution more then 1h 😀

Related posts

Leave a Reply

1 comment

  1. 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.