Show Only Selected Categories on the Blog Page
Limits the main blog page to posts from the categories you choose, for example only News, while other categories keep their own archives.
Opens WPIDE on
Code
// Category slugs to show on the blog page, for example array( 'news' ).
$wpide_blog_categories = array();
add_action( 'pre_get_posts', function ( $query ) use ( $wpide_blog_categories ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_home() || ! $wpide_blog_categories ) {
return;
}
$query->set( 'category_name', implode( ',', array_map( 'sanitize_title', $wpide_blog_categories ) ) );
} );
Notes
The list is empty by default, which changes nothing. Add category slugs; posts from other categories still appear in their archives, search and feeds.
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.