Restrict back-end post and page view - Role restriction

Hi,

I have created a function that only allow the user "author" to see his/her own posts. The post view is based on the users ID. However, is there any bright person that knows how I can make the restriction role based - so that all authors only can see their post, so editors only can see their posts and so on.

Here is my code: https://gist.github.com/mikakaltoft/92a3d4d1069c691111ce

I tried with $wp_query->set( 'role', 'editor' ); however, that doesn't seems to do the trick.

Sincerely,
Mika

  • Rupok
    • Support Ninja

    Hi Mika

    I hope you had a wonderful day.

    Well, I think I've a workaround for you. You can get the list of all ids that belongs to the role of the logged in user by the following function. Then you can run a loop for fetching posts for all users in your function.

    function author_ids_by_role() {
            global $current_user;
            $user_roles = $current_user->roles;
            $user_role = array_shift($user_roles);
            $ids = get_users(array('role' => $user_role ,'fields' => 'ID'));
            return $ids;
            // print_r($ids);
    }

    Can you please try and confirm? Please let us know if you have any further confusion. We will be glad to help.

    Have a nice day. Cheers!
    Rupok

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.