Exclude a Category From the Blog Page
Hides posts in chosen categories from the main blog page. The posts remain visible in their own category archive.
Opens WPIDE on
Code
add_action( 'pre_get_posts', function ( $query ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_home() ) {
return;
}
// IDs of the categories to hide.
$excluded = array( 1 );
$query->set( 'category__not_in', array_merge( (array) $query->get( 'category__not_in' ), $excluded ) );
} );
Notes
Replace 1 with your category IDs (1 is Uncategorized on most sites).
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.