Hide draft and pending posts from other authors

Hide drafts and pending posts

Hide drafts and pending posts

You don’t like that contributors and authors of your multi-authored WordPress blog see not published post titles of other users. You wish to hide from user all posts in the ‘draft’ or ‘pending’ states. You are in the right place. Let’s see how to achieve this.
WordPress has filter ‘views_edit_post’ where you can modify the list of views available to the user when he clicks ‘Posts” or ‘All Posts’ links at his dashboard. Available views are: ‘all’, ‘draft’, ‘mine’, ‘pending’, ‘publish’. So in order to permit user to see just his own posts in any state and others posts in ‘published’ state only we should exclude ‘all’, ‘draft’ and ‘pending’ views. Let’s see to the code:

1
2
3
4
5
6
7
8
9
10
11
12
function mine_published_only($views) {
 
  unset($views['all']);
  unset($views['draft']);
  unset($views['pending']);
 
  return $views;
}
 
if (current_user_can('contributor') || current_user_can('author')) {
  add_filter('views_edit-post', 'mine_published_only');
}

Insert it to your theme ‘functions.php’ file and check. Users with roles ‘Contributor’ or ‘Author’ will see ‘Mine’ and ‘Published’ links only above posts list.

Be aware that this technique just hide from user interface elements. He still can type in the browser direct link, e.g.
http://www.yourblob.com/wp-admin/edit.php?post_type=post&all_posts=1
and see all post titles in any state.

In order to not only hide but block access to the other authors not published post titles your can use this extended variant

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function mine_published_only($views) {
 
  unset($views['all']);
  unset($views['draft']);
  unset($views['pending']);
 
  return $views;
}
 
function only_own_posts_parse_query( $wp_query ) {
    if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) {
      global $current_user;
      $wp_query->set( 'author', $current_user->id );
     }
}
 
if (current_user_can('contributor') || current_user_can('author')) {
  add_filter('views_edit-post', 'mine_published_only');
  add_filter('parse_query', 'only_own_posts_parse_query' );
}

We add ‘author’ parameter with current user ID value to every request of ‘edit.php’ file. Thus user will have access to his only titles.

Tags: