Archived topic
How to hide some posts from blog page
7 replies · Started by Matteo on January 3, 2021
I'd like to create a new posts category and hide those posts (the entire category) on the blog page.
The posts will exist and they will be in the sitemap index, but they should be invisible to readers.
How can I do?
Hi there,
you can use this PHP Snippet to exclude a category:
add_action( 'pre_get_posts', function( $query ) {
if ( !is_admin() && $query->is_main_query() ) {
$query->set( 'cat', -10 );
}
} );
the 10 value is the Category ID. You can get the ID by editing the Category and checking the URL.
This code hide also the post from Posts-All Posts in WP dashboard and I can't find the post.
Updated the code above to fix that.
Resolved with this code:
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-55' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
Where 55 is a random category number to change.
Glad to hear you found the answer
Also your code works. Thanks.
You're welcome