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.
add_action( 'pre_get_posts', 'themeprefix_exclude_category' ); // 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; }
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…
add_action( 'pre_get_posts', 'only_portfolio_category' ); // Only Portfolio Category function only_portfolio_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '3' ); } }
So in this example, only the posts in a category id of ‘3’ would show.
10 comments
Naji Khan
Hi,
Is there any WordPress plugin to exclude the specific category and hide from home page?
anne
I have a blog on one page. I want it on two pages. Blog categories (and posts) 1-7, I would like:
Page X excludes blog categories (and posts) 1, 2, 3.
Page Y excludes blog categories (and posts) 4, 5, 6, 7.
I tried using your code and altering it myself. I have no experience and it didnt work. Any chance you can help.
I found out
Page ID “post=195”
Page ID “post=182”
And also found out all the category IDs
Thank you for a great post :)
Andy
Thank you!!!! That just saved me a whole bunch of work
Olivia
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
Carla
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!
Neil Gee
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
Santosh
Great! Thanks a lot!
Vicente
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
This sounds more like a PHP syntax error – look for the line number the error gives you and see if you have one extra ‘”‘
Joshua James
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.