Contributor disable seeing others’ posts

How can I disallow contributors on my site from seeing what other posts are published on the site, and only see their own?

Related posts

Leave a Reply

3 comments

  1. I hope you are talking about wp-admin section. If yes just place this code in your functions.php file

    add_action( 'load-edit.php', 'posts_for_current_contributor' );
    function posts_for_current_contributor() {
        global $user_ID;
    
        if ( current_user_can( 'contributor' ) ) {
           if ( ! isset( $_GET['author'] ) ) {
              wp_redirect( add_query_arg( 'author', $user_ID ) );
              exit;
           }
       }
    
    }
    
  2. @Giri: Thanks for the answer but it’s not perfect yet.

    Admin can modify the query arg “author” manually and they will be able to view the other

    if ( ! isset( $_GET['author'] ) ) {
    

    The line above should also check if $_GET[‘author’] is not current user

  3. Another approach, like accepted answer (but without redirection):

        function posts_for_current_author ($query) {
            if( $query->is_admin && 'edit.php' == $GLOBALS['pagenow'] && !current_user_can( 'edit_others_posts' ) ) {
                $query->set('author', $GLOBALS['user_ID'] );
            }
            return $query;
        };
        add_filter('pre_get_posts', 'posts_for_current_author');