Skip to content

Instantly share code, notes, and snippets.

@mojaray2k
Last active August 29, 2015 14:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojaray2k/b9895ed7d5f1340cafee to your computer and use it in GitHub Desktop.
Save mojaray2k/b9895ed7d5f1340cafee to your computer and use it in GitHub Desktop.
This code snippet is the basis for restricting content for users in the WordPress Admin

##Restrict Post Editing in the WordPress Admin

This code allows any users with the capability to edit other’s posts to view all posts. This means editors and administrators will be able to see all posts. Users with other roles like contributor or authors will only see their own posts. If you are using custom user roles on your site, then you need to keep in mind that users who can edit posts added by other users will also be able to see them.

<?php
function posts_for_current_author($query) {
global $pagenow;
if( 'edit.php' != $pagenow || !$query->is_admin )
return $query;
if( !current_user_can( 'edit_others_posts' ) ) {
global $user_ID;
$query->set('author', $user_ID );
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment