Hide a certain Category’s Posts from the Home/Blog page in WordPress

To hide a certain Category’s posts from the home or blog page you just need to know the category ID and then use a filter on the pre_get_posts hook to exclude those posts.

To find the ID for the Category just go to the Category and hover over the edit button and the ID appears in the URL at the bottom.

wordpress-category-id

// Exclude Category Posts from Home Page
function themeprefix_exclude_category( $query ) {
	if ( $query->is_home() ) {
	$query->set( 'cat', '-338' );//add your category number
	}
	return $query;
}
add_action( 'pre_get_posts', 'themeprefix_exclude_category' );

Then add the function inside your functions.php file. The above function only runs on the home page and will remove any posts that belong in the category numbers listed. In the example above that would be number 338, to exclude more than one category just comma separate all the ID numbers like so; ‘-338, -340, -350’

 

The reverse of this is also true, if you wanted to only show a particular category then use the same code but without the minus…

// Only Portfolio Category
function only_portfolio_category( $query ) {
   if ( $query->is_home() && $query->is_main_query() ) {
   $query->set( 'cat', '3' );
   }
}
add_action( 'pre_get_posts', 'only_portfolio_category' );

So in this example, only the posts in a category id of  ‘3’ would show.

7 Comments

  1. Olivia on February 17, 2016 at 1:39 pm

    Hello

    i have added

    //Exclude Category Posts from Home Page
    function themeprefix_exclude_category($query) {
    if ( $query->is_home() ) {
    $query->set(‘cat’, ‘-4,-3,-5,-6’);//add your category number
    }
    return $query;
    }
    add_action(‘pre_get_posts’, ‘themeprefix_exclude_category’);

    yet the home page is still showing all categories – is there anything you can add to this to help at all?

    many thanks

  2. Carla on October 13, 2015 at 3:27 am

    Thanks for the tutorial.

    I have a category of posts I wanted to completely exclude from displaying in my blog. They are set up to only display on a certain page.

    You said this addresses the homepage, which to me is the page you see when you land on someone’s blog for the first time. But much to my surprise it excludes all of that category from the entire blog, which is exactly what I wanted.

    Could you please help me understand why it works, when only ‘home’ is referenced in the code? Thank you!

  3. Santosh on September 25, 2015 at 5:50 am

    Great! Thanks a lot!

  4. Vicente on September 13, 2015 at 9:14 am

    Hi, this code used to work fine before the latest WP update (4.3), but it now produces the following error:

    Parse error: syntax error, unexpected ‘’’ (T_STRING) in /home/example.com/wp-content/themes/exampletheme/functions.php

    How could I fix it? Thanks!

    • Neil Gee on September 13, 2015 at 11:00 pm

      This sounds more like a PHP syntax error – look for the line number the error gives you and see if you have one extra ‘”‘

  5. Joshua James on May 20, 2015 at 1:35 am

    Thank you. This saved me a bunch of time. Prior to this I didn’t know that I could filter the posts query on the functions level. super powerful trick. I will definitely use this a lot in the future.

Leave a Comment





%d bloggers like this: