Only show post from current user with author role

Hey

Users with the role "author", should be able to see all posts, except posts from other users with the the same role of author.

How can I accomplish to do that?

This code:

add_filter('parse_query', 'rocapress_extend_author_user_role' );
function rocapress_extend_author_user_role( $wp_query ) {

    if ( current_user_can( 'author' )  )  {

        global $current_user;

        $wp_query->set( 'author', $current_user->ID );

    }
}

Will limit the post being displayed on the front-end to the current user alone. So that is not quite right, but I believe it has elements that we need to use to reach the desired solution.

  • Sajid

    Hi @Mika,

    Hope you are doing good today :slight_smile:

    I would go with this code instead. Try it out and let me know the results.

    add_filter('pre_get_posts', 'rocapress_extend_author_user_role' );
    function rocapress_extend_author_user_role( $query ) {
    
        if ( is_user_logged_in() and current_user_can( 'author' ) )  {
    
            $user_ID = get_current_user_id();
    
            $query->set( 'author', $user_ID );
    
        }
    }

    Its not tested so be careful.

    Hope that helps! If you still need assistance, don't hesitate to ask.

    Take care and have a nice day :slight_smile:

    Kind Regards,
    Sajid J

  • Sajid

    Hi @Mika,

    Hope you are doing good today :slight_smile:

    Yes, I got your point now. In this scenario get IDs of the all users with author user role except the current one and pass that array to the author_not_in argument. This will return posts from current user with author role and all other users with other user levels like admin.

    add_filter('pre_get_posts', 'rocapress_extend_author_user_role' );
    function rocapress_extend_author_user_role( $query ) {
    
        if ( is_user_logged_in() and current_user_can( 'author' ) )  {
    
            $user_ID = get_current_user_id();
    
            //Get all author's IDs except the current logged in author user
            $args = array(
            				'role'		=> "author",
            				'exclude'	=> array( $user_ID ),
            				'fields' 	=> array( 'ID' ),
            			); 
    
            $blogusers = get_users( $args );
    
            //now don't show posts from these authors
            $query->set( 'author__not_in', $blogusers );
    
        }
    }

    Again not tested but should work.

    Hope that helps! If you still need assistance, don't hesitate to ask.

    Take care and have a nice day :slight_smile:

    Kind Regards,
    Sajid J

    • Mika

      Hey Sajid

      Thanks for getting back to me so quick!

      It worked, almost. I made a few tweaks and it works now :smiley:

      /* Exclude other authors from seeing each others posts */
      add_filter('parse_query', 'rocapress_exclude_authors' );
      function rocapress_exclude_authors( $query ) {
      
          if ( is_user_logged_in() && current_user_can( 'author' ) )  {
      
              $user_ID = get_current_user_id();
      
              //Get all author's IDs except the current logged in author user
              $args = array(
                              'role'      => 'author',
                              'exclude'   => $user_ID,
                              'fields'    => 'ID',
                          ); 
      
              $blogusers = get_users( $args );
      
              //now don't show posts from these authors
              $query->set( 'author__not_in', $blogusers );
      
          }
      }

      Thanks a lot :slight_smile:

      Sincerely,
      Mika

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.