Theme & Front End PHP Beginner

Exclude a Category From the Blog Page

Keep posts from one category, such as News or Podcast, off the main blog page while leaving them in their own archive.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 10 lines
add_action( 'pre_get_posts', function ( WP_Query $query ): void {
	if ( is_admin() || ! $query->is_main_query() || ! $query->is_home() ) {
		return;
	}

	$category = get_category_by_slug( 'podcast' );
	if ( $category ) {
		$query->set( 'category__not_in', [ $category->term_id ] );
	}
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Some posts don’t belong in the main blog feed, such as short announcements or podcast episodes with their own page. This snippet hides one category from the blog page while keeping it everywhere else.

How it works

The pre_get_posts action lets you change the main query before WordPress runs it. The snippet only acts on the front-end blog page (is_home()), looks up the category by its slug and tells the query to leave it out with category__not_in.

The category’s own archive, search results and feeds aren’t affected.

Things to watch

  • Posts in two categories are hidden if either one is excluded.
  • To exclude several categories, add more term ids to the array.
  • Blog post blocks and widgets run their own queries and have their own settings.

How to test it

Open your blog page. Posts in the excluded category should be missing, but they should still appear on that category’s page.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only