• Resolved pmb88

    (@pmb88)


    I am trying to implement this on my website. I have configured the items to show on certain logged roles. However, on the front end list page, it’ll show the entry, even when not logged in. The admin configurations only show the preview of the restricted item on the list. There is not any that I can find that hides the restricted content from the list. Is there a way to configure the list to only show that restricted item if they’re logged in?

    • This topic was modified 4 years, 1 month ago by pmb88.
    • This topic was modified 4 years, 1 month ago by pmb88. Reason: grammar
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support alexandrubodea

    (@alexandrubodea)

    Hi @pmb88,

    What do you mean exactly by the “front end list page”?

    Could you also please send a link to the page where you encounter the issue?

    Best regards,

    Thread Starter pmb88

    (@pmb88)

    To clarify, what I meant is the blog list page.

    For example, you have a post and it’s limited to subscriber. So anyone that isn’t a subscriber can’t view that post if they accesses it via the url. However, if you go to the blog list page as someone other than subscriber, the post is still listed. My query is how to filter the list so that it only shows up if subscriber is logged in?

    Plugin Support alexandrubodea

    (@alexandrubodea)

    To hide the listed posts to the users that do not have the role to view them, you need to use the following code:

    add_action( 'pre_get_posts', 'wppbc_exclude_posts_from_query' );
    function wppbc_exclude_posts_from_query( $query ) {
    
        remove_action('pre_get_posts', 'wppbc_exclude_posts_from_query');
    
        if( !function_exists( 'wppb_content_restriction_is_post_restricted' ) || is_admin() || is_single() )
            return;
    
        if( $query->is_main_query() || ( $query->is_search() && isset( $_GET['s'] ) ) ) {
            $args = $query->query_vars;
            $args['suppress_filters'] = true;
            $args['posts_per_page'] = get_option( 'posts_per_page' );
            $posts = get_posts($args);
    
            $ids = wp_list_pluck( $posts, 'ID' );
            $restricted_ids = array_filter($ids,'wppb_content_restriction_is_post_restricted');
    
            $query->set( 'post__not_in', $restricted_ids );
        }
        
    }

    You can use the code by adding it to your theme’s ‘functions.php’ file or by creating a new plugin as described here.

    Thread Starter pmb88

    (@pmb88)

    This was exactly what I needed. Thank you for your assistance.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Hide content from list’ is closed to new replies.