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?
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.