How to limit authors to their own posts in WordPress admin

If you have a multi-user blog you maybe have this problem that the authors can see all the posts when they only must see their own posts. So How to limit authors to their own posts in WordPress admin?

Here an image explains the problem of the post list on admin:

posts in WordPress admin

 

Although the author can only edit his posts, it is better than for default the other posts don’t appear.

 

How avoid the authors see all the posts in WordPress

We use the pre_get_posts  hook for this purpose. But! you must be very careful with this hook because it affects the entire web (backend and frontend). If you use the correct conditions everything will be fine.

 

 

Code explanation

The first conditional: will apply the script if the user is in the post list on admin. If he is not in this section so the script must no apply.

The second conditional: will apply the script if the user has no permission to edit another post because if the user can edit others is probable that this user is edit or administrator.

Finally, the script gets the ID of the current user (with active session) in the $user_ID variable and sets it to the query, so the list now has a new filter ( or where statement).

 

Result:

Now when the author goes to the post list on admin, he will see only his posts.

 limit authors to their own posts in WordPress

If you liked this article, please share it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top