Only show your own posts in admin (hiding others’ posts from contributors)

I’ve written some themes and plugins that make extensive use of WordPress custom post types. These are coupled with new roles that lie somewhere between contributor and editor. The member can publish some post types, submit others for review. It has raised the problem that I do not want these members to see other members’¬†posts. The reasons for this are twofold. Firstly the admin screen will get busy, there will be too much noise, this will usually be too complex for them to handle. Secondly I don’t want them looking at other member’s¬†unpublished posts.

The answer is to filter the global $query object, restricting the member to their own posts. This can either be white or black labelled depending on how you wish to go.

 * This restricts posts in wp-admin for Subscribers
 *  and Contributors to their own posts only.
 * However, none of the admin-table-filters hookable into,
 *  so you'll need some CSS too.
add_filter('pre_get_posts', 'tcb_admin_show_only_authors_posts');
function tcb_admin_show_only_authors_posts($query) {
  global $current_user;
  $post_type          = $query->get('post_type');
  $allowed_post_types = array();
  if ( in_array($post_type, $allowed_post_types) ) return $query;

  $restricted_roles = array('subscriber', 'contributor');
  $user_roles = $current_user->roles;
  $role       = reset($user_roles);
  if( $query->is_admin && in_array($role, $restricted_roles) ){
    global $user_ID;
    $query->set('author', $user_ID);
  return $query;

The filter only works for the actual editable content. The (useful) menu across the top allowing people to select mine or published uses some separate SQL, and there are no hooks in the code. So these must be hidden using CSS.

This should get you started:

 display: none;