Prevent Authors from viewing each others Posts

I am setting up a site where there will be multiple users as Author, the owner doesn’t want the authors to be able to view each others posts, since there are some meta fields with information he’d rather not have shared between the authors.

Is there a way to remove the ability to view other authors posts?

Read More

Thanks,
Chuck

To clarify a bit more, this is for the admin side, at the top underneath Posts, there are links for mine, all, and published. I only want Authors to see “mine”.

Related posts

Leave a Reply

5 comments

  1. If you want to prevent a user with the “Author” role to view other users’ posts in the overview screen (they won’t be able to view the details anyway), you can add an extra filter on the author:

    add_action( 'load-edit.php', 'wpse14230_load_edit' );
    function wpse14230_load_edit()
    {
        add_action( 'request', 'wpse14230_request' );
    }
    
    function wpse14230_request( $query_vars )
    {
        if ( ! current_user_can( $GLOBALS['post_type_object']->cap->edit_others_posts ) ) {
            $query_vars['author'] = get_current_user_id();
        }
        return $query_vars;
    }
    

    The little links above the post table (“Mine”, “All”, “Drafts”) are less useful now, you can also remove them:

    add_filter( 'views_edit-post', 'wpse14230_views_edit_post' );
    function wpse14230_views_edit_post( $views )
    {
        return array();
    }
    
  2. Just check for capabilities (See link from @Wyck) & the author ID inside your templates and put the stuff you don’t want others to see inside an if/else check:

    // Get the author of this post:
    $post_author = get_query_var('author_name') ? get_user_by( 'slug', get_query_var('author_name') ) : get_userdata( get_query_var('author') );
    
    // Get data from current user:
    global $current_user;
    get_currentuserinfo();
    // Get the display_name from current user - maybe you have to exchange it with $current_user->user_login
    $current_author = $current_user->display_name;
    
    // Check the capability and if the currently logged in user is the the post author
    if ( current_user_can('some_capability') && $post_author == $current_author )
    {
        // Post Meta
        $post_meta = get_post_meta( $GLOBALS['post']->ID );
        // DO OR DISPLAY STUFF HERE
    }
    
  3. I had to do something like this today and this is why I found this post. What I found that worked for me was this post titled: “How to Limit Authors to their Own Posts in WordPress Admin” by wpbeginner

    Here is the code that you can paste on your functions.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');