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:

  • http://twitter.com/MrIanBelanger Ian Belanger

    Hi,

    I am using your role editor plugin on my site. This is what I am looking for.

    I want my users to only see their pages when they go into the pages list, published or not.

    I can use your plugin to set it so they can only edit their own pages, but all of the pages from all users still show in the list.

    Can this be done?

    Thanks

  • http://shinephp.com Vladimir Garagulya

    Hi,

    It’s possible with the same technique as described in the post above, just for pages type use another filter
    ‘views_edit-page’ and do not try unset  ‘pending’ views as ‘pending’ status is not applicable to ‘pages’ type.
    For example:
    function mine_pages_only($views) {
     
    unset($views[‘all’]);
    unset($views[‘draft’]);
     
    return $views;
    }
     
    function only_own_pages_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(‘page_creator’)) {
    add_filter(‘views_edit-page’, ‘mine_pages_only’);
    add_filter(‘parse_query’, ‘only_own_pages_parse_query’ );
    }

    Where ‘page_creator’ is the name of your role.

  • aditia

    thank you work really nice, it would better if it added on plugin, maybe user role editor

  • http://shinephp.com Vladimir Garagulya

    Thanks. I will think about it.

  • Gabor Miglecz

    Hi Vladimir, you’re a king, it works seamless.
    An aesthetic question (because looks a bit equivocal), is it a hard question that how to display not the full number of really existing posts in brackets but that owner’s own posts’ number?
    Now it looks like: Mine(3) All(8) Published(6) Draft(2) – and of course i can click any of them, i see only my own ones. Thanks in advance, Gabor Miglecz

  • http://shinephp.com Vladimir Garagulya

    Hi Gabor,

    Thanks for so good words.
    I’m sorry, but I don’t see how to achieve what you wish for this moment. Left just “Mine” view using

      unset($views['all']);
      unset($views['draft']);
      unset($views['pending']);
      unset($views['publish']);

    and you will not see such mess with posts counters.

  • william adams

    I’m having the opposite problem.  I want my authors and contributors to see each others’ pending and draft posts so they don’t write about the same thing.  However, they can only see their own posts.  BTW, I was using the User Role Editor plugin but in conflicted with my Mailpress plugin so I deactivated it.  I reactivated it to see if I had somehow blocked such author post viewing but only found ‘edit’ and ‘pubhlish’ functions.  Anyway, how can I accomplish what I want?

  • http://www.facebook.com/bhattacharyyasoumik Soumik Bhattacharyya

    Can you figure out a method that prevents users from seeing pending comments in the dashboard?

  • Nam Phương

    this code work for me :)

    “function mine_published_only($views) {

    unset($views[‘all’]);
    unset($views[‘trash’]);
    unset($views[‘moderated’]);
    return $views;
    }

    if (current_user_can(‘contributor’) || current_user_can(‘subscriber’)) {
    add_filter(‘views_edit-post’, ‘mine_published_only’);
    add_filter(‘views_edit-comments’, ‘mine_published_only’);
    }

  • Evgeny

    Hi Vladimir! This is exactly what I’m looking for! I’m using your plugin now, but it looks like that hiding the drafts and pending posts is possbile via coding and not in the plugin. Would it be possible for your to help me with this coding? Will highly appreciate your time!

  • http://shinephp.com/ Vladimir Garagulya

    Hi Evgeny,

    I will answer on your email.

    Regards,
    Vladimir.

  • Petera

    Thank you Vladimir. You just saved my life. I was frustrated with people talking about other people’s pending blogs. I just added the code in the functions.php and it works. I logged in as a contributor and tested the function and it works as intended. I really appreciate your help.

  • http://shinephp.com/ Vladimir Garagulya

    Excellent! Thank you Petera for the good feedback.

  • Laura Lee

    Good article. I am admin for a multiauthor site. I want to “hide and unhide” all other authors with a toggle… because I do need to see pending articles because I have to publish them. Go back and forth between two different subsets. I don’t want all of this in my article feed all the time. I am really thinking about creating another profile for myself that I will post out of… so I have my own space. Other authors stay in my feed even after they are published. I really want two different post feeds: one for me that’s my authorship… one for others that is just that work of pending and published. I have to see these articles and approve them. I am going to have to be 2 different users to accomplish that end it appears.
    ***K, I just set up an Author user for myself as a 2nd user so I will only see my own posts. On the Author side… it has a “Mine” and “All”… but it didn’t on the admin panel. That’s all I wanted was a “Mine” and “All” I could go back and forth with. I don’t get it. I don’t get why they didn’t do that for the admin. The admin doesn’t have a “Mine” area… and an “Author” does? So, what I need really is a “Mine” field on the admin panel as Admin. **Just trying to finish this site…. learning as I go.

  • DM Productions

    Taking it just a little farther:

    function mine_published_only($views) {

    unset($views[‘all’]);

    unset($views[‘draft’]);

    unset($views[‘pending’]);

    unset($views[‘trash’]);

    unset($views[‘moderated’]);

    unset($views[‘spam’]);

    return $views;

    }

  • Still Youth

    That’s good. Is there a way to hide some categories too? e.g. dedicate special categories for contributors only.

  • http://shinephp.com/ Vladimir Garagulya