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.
// 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.
Thank you!!!! That just saved me a whole bunch of work
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
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!
The ‘home’ refers to the blog page when used as the home page and the blog page itself. – https://codex.wordpress.org/Function_Reference/is_home
Great! Thanks a lot!
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!
This sounds more like a PHP syntax error – look for the line number the error gives you and see if you have one extra ‘”‘
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.