• Resolved jonathanisaac

    (@jonathanisaac)


    Right now when a user views an archive page that includes some posts they don’t have access to, they see the “Sorry, but you do not have permission to view this content.” message under the post title. I’d rather hide the entire post from them so they can’t see the title or the message on the archive page. Is this possible?

Viewing 1 replies (of 1 total)
  • Plugin Author Caseproof

    (@caseproof)

    Hi @jonathanisaac

    The content permissions tool does not allow to remove protected content from archive page. As a workaround, you could use pre_get_posts hook to filter protected posts. I’ll attach a code snippet that uses this hook, but you might need to modify it for your needs:

    add_action( 'pre_get_posts', function ( $query ) {
      if ( ! is_admin() && $query->is_main_query() && 'blog' === $query->query['pagename'] ) {
        $posts = get_posts( array( 'post_type' => 'post', 'post_status' => 'publish', 'numberposts' => - 1 ) );
        if ( empty( $posts ) ) { return; }
        $protected_posts = array();
        
        foreach ( $posts as $post ) {
          if ( ! members_can_current_user_view_post( $post->ID ) ) {
            $protected_posts[] = $post->ID;
          }
        }
        if ( empty( $protected_posts ) ) { return; }
    
        $query->set( 'post__not_in', $protected_posts );
      }
    } );

    I hope that helps.

Viewing 1 replies (of 1 total)
  • The topic ‘Hide Restricted Content in Archives’ is closed to new replies.