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

- Type: PHP
- Runs: Run everywhere
- Category: [Archives](https://wpide.com/library/category/archive/)
- Tags: blog, categories, homepage
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/blog-page-only-selected-categories/

## Code

```php
// 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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Show Only Selected Categories on the Blog Page" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
