Hi @bcourcet
We don’t have any option to hide protected posts on a listing page like Blog page. You can try to use this code snippet to exclude those posts on Blog page:
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 );
}
} );
To hide posts from search results, you can try this plugin: https://ww.wp.xz.cn/plugins/mpress-hide-from-search/
I hope that helps.
Hello,
and thank you for the quick reply. But may i ask you what do you call a “protected post” ?
Thank you
By protected posts I mean posts protected with Content Permissions feature in Members plugin: https://members-plugin.com/docs/content-permissions/
When you protect posts this way, their content is protected, but they are still being displayed in the posts listing on Blog page. The method I sent you allows you to override it and remove those posts from listing page.
I hope that makes sense.
Hi! Thanks for that code snippet.
I was wondering if it will work for another page (not the blog page).
I wanted to create a new Page “restricted content” where to display the posts from an specific category.
So you know what should I modify in the code snippet in order to work?
Thank you very much!
Hi @caseproof !! do you think could yo help me with this please?
Thanks a lot!!