How to exclude categories from the home page of your WordPress blog
There are lots of reasons you might want to exclude one particular category from your blog's front page. Today, for example, I wanted the "Quote of the Day" on WhoTube to be only in the sidebar, not in the main column of content. In my personal blog, I want to exclude my Twitter archives from the front page. And happily, this is a very easy thing to do; you don't even need a plugin, just one little theme edit.
Firstly, you need to find the number of the category you want to exclude:
- go into Posts > Categories
- click on the relevent category name
- the URL of the next page should end &cat_ID=xx, where xx is a number. Make a note of it.
Next, go into Appearance > Editor, and open functions.php. Add the following function:
function exclude_category($query) {
if ( $query->is_home ) {
$query->set('cat', '-xx');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
where xx is the number of your category. Don't forget the minus sign in front of it! Save it, and you're done.
Excluding multiple categories
is easy. Just add the extra category numbers, making sure you have a minus sign in front of each one:
$query->set('cat', '-1 -2 -3 -4 -5');
Excluding categories from archive pages
is also easy. The second line of the code above says "if the query's being run on the home page"; just change it to say "if the query's being run on an archive page":
if ( $query->is_archive ) {
or if you want home page and archive pages to exclude the category:
if ( $query->is_home || $query->is_archive) {
Tags: customization, themes, WordPress
Posted by Sue on April 17, 2009 in WordPress.








Thanks to the person who left me the comment: I've corrected the code above, and I apologise most profusely for having missed a line, but I'm not going to let your filth stand.
Hi
I am trying to exclude my shop categories from my home page but not my featured section (the slider right beneath the slide show) Do you have any ideas or suggestions for that?
Any help would be very much appreciated
Hi Yemoonyah,
Presumably the top section is generated by one loop and the lower section by another? I'm guessing here - almost impossible to be more specific without looking at the code behind your blog - have emailed you.
hi,
i have been hunting for this endlessly. typed in a different serch query and found this page.
I was just about to comment with a big question..... but i tried inserting once more and IT WORKED!!!!!!!!!!!!!!!!!!!!!!!
WOW
thank you so much, holy crap so happy!
J
I want to say big thanks to you .
your code made my life easier .
thanks
Adnan
Thanks Adnan
This was awesome. Worked perfect and was easy. I tried 5 different plugins before finding this post, and none of them worked.
I have the site http://www.pd4free.com and I am using 2.8 wordpress. I tried using this solution and I am not getting it to work. I put all the code in the function.php file. Should I put the add_filter somewhere else?
Thank you
Sean, I think your site has other problems which you need to resolve first: specifically, the fact that you have the same two posts over and over. Fix that before you try and do anything else.
But to answer the specific question, no, you put all the code into functions.php.
Thx Sue, was very simple.
Hi Sean, make sure to put the code inside the <?php tag like this -
is_home ) {
$query->set('cat', '-1 -15');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
Hi
Where in functions.php should this code be placed. Right t the top?
Hi Nigel:
Immediately after the first <?php is probably the safest place to paste it.
Where is functions.php? I'm using a custom template.
Hi Marc,
If your theme doesn't have a functions.php, you can do the following:
1) create a new file in Notepad or similar text editor (NOT Word!)
2) post the code above into it
3) put <?php right at the beginning of the file, and ?> at the end.
4) upload it to wp-content/themes/YOUR THEME FOLDER
5) check in Appearance > Editor that it's there
and it should work okay.
Wow great I knew there had to be an easier way of doing this. Thanks for the info. I had to mess with the loop before.
Below is the code I'm using with Wordpress 2.8. I'm trying to make the category so it only shows up in that category and it not in the blog (home), archives, or the feed. With the code below, it is excluding the postings from the blog (home) and the feed (ie recent posts, etc.), but it still has the posts listed under the archives page "by post". It does take it out of Archives under the date though. Anyone have any suggestions?
function exclude_category($query) {
if ( $query->is_home || $query->is_archive || $query->is_feed ) {
$query->set('cat', '-5');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
Jason, if I understand correctly what you’re trying to do, you probably want to throw an is_single in there too.
Great work around, this worked well for me.
I had found and used other ways to exclude certain categories from certain things, but none of those were fixing the issue I was having. When excluding a category from the front page, it was still being counted in the post total for the front page. So if I wanted 10 posts displayed and the excluded category accounted for 6 of the last 10 posts, only 4 posts showed on my front page.
This code fixed it so that 10 posts show while excluding a category. Thank you.
Cool, it did what it's supposed to do then
Glad you found it useful Jason.
Hello,
I would like to have multiple pages of posts, not just two. Is there a function off of $query for is_page('x')?
function exclude_category($query) {
if ( $query->is_page('8') ) {
$query->set('cat', '-5,-9');
}
elseif ( $query->is_page('10') ) {
$query->set('cat', '-6,-3');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
Otherwise, this works great. A much fast routine than the plug-in "Frontpage Exclude Categories".
Thank you,
Brad
Hi Brad,
Not quite sure what you mean by
I would like to have multiple pages of posts, not just two
If you want to change the number of posts shown on a page, that's under Settings > Reading.
is_page is for a WordPress page, not an archive page, so is_page('8') is testing for whether you're on page with the post number 8. Similarly you can do is_page('About Me') with the title, or is_page('about-me') with the slug.
Also (forgive me if this is obvious) - WordPress can't display posts that aren't there. If a category has no posts, you won't see it; if it only has one post, you won't see pagination. This is a frequent cause of panic and strife in setting up new blogs
Perhaps I've totally misunderstood what you're trying to do - can you explain in a bit more detail - feel free to Skype me or email if you like -
http://blogmum.com/contact
Best,
Sue
I am excluding my product category from my blog area by using this code to start the loop:
<?php
$child_cats = (array) get_term_children('114', 'category'); // array_merge(array('114'), $child_cats)));
if(have_posts()) : while(have_posts()) : the_post();
?>
But the products still show up in the Date based archive (see August). How can I exclude the product category from these archives? I tried your solution and it works for the archives, BUT it causes a page not found error when browsing to the product pages, which are category archives.
Thanks for your help!!
Hi Rick,
Probably the best way to achieve what you want is to create specific category archives, and just exclude the product category from the more general archives, which you create using archive.php. If you're not familiar with the template hierarchy then have a read here:
http://blogmum.com/2009/08/how-a-wordpress-theme-works/
Let me know if you need more help!
Ciao,
i'm using this code to exclude a category from homepage:
php
if (is_home()) {
query_posts("cat=-77");
}
that work fine.
But i want one more thing, the excluded category to display only the last post. Any help or suggestion?
thanks
Giuseppe
Ciao Giuseppe,
You'll need to put another loop in there somewhere. Personally I'd have the last post from cat 77 at the beginning/end, and use a new WP_Query() to just pull out the most recent post. Let me know if that doesn't achieve what you want!
Sue,
thank you. I try with this before the loop:
if (is_home()) {
query_posts("cat=-77");
query_posts("showposts=1");
}
and it let me display only last post from cat 77, but all other post from other cat aren't displayed.
ciao
showposts=1 will always get you only one post
What your code says is "don't show me category 77, and only show me one post", but tbh I'm not sure if you can even do two query_posts like that.
To be safe, as above, you need two loops. So maybe for the first
if (is_home()) {
query_posts("cat=-77&showposts=1");
}
And then another one showing whatever else you want. You need to read this
http://codex.wordpress.org/The_Loop#Multiple_Loops_Example_1
on multiple loops on the same page.
Sue,
thanks, but after various attempts, nothing to do for me.
I'm going to apply more time on the codex.
ciao
Giuseppe
Well, I'm sorry to say it - I can certainly fix it for you, but you'll have to pay me to do the work.
Sue,
i'll keep you in mind if i want to do it so.
ciao
Excellent piece of code. It took me some time to try and explain to Google what I wanted, but I was eventually led here and appreciate the post.
That said, I have an issue. Code works great. I've added a post to my site that's not appearing on the homepage, just as I want. However, when I click on the category I created to exclude, it doesn't show my photos. Pics are def. in the post itself and are there when you actually click on the post, but they're missing from the category page.
Any ideas?
Steve
Not sure that's an issue with the excluding category thing, Steve - it's hard to imagine a mechanism to link the two things. Can you leave a link to the actual category page and I'll take a look?
Sure.
http://weirdvegas.com/category/around-weird-vegas/
When you click in the actual category, the photo isn't shown. But when you look at the post, the photo is there.
http://weirdvegas.com/2009/10/ahhhhh-zombies/
Thanks for your help. Forgive the example picture... It was zombie day here in LV.
Are you kidding, that's a superb picture!
So I think perhaps your archive.php is using the_excerpt() rather than the_content() - none of your category archives have pictures, e.g.
http://weirdvegas.com/category/people/
Have a look at that file in Appearance > Edit - it's the most likely reason, and that [...] on the end of the snippet for "Zombies on Fremont Street" looks like the_excerpt() to me.
Haha! Goes to show how often I look at my cat pages...
I don't see the change I need to make. I tried pasting my code in but it didn't allow. I'll research further and see what I can come up with.
Thanks for your help.
You're welcome to try it but I think the comments thing will eat your code. Better to save it as a text file and mail it to me - sue@blogmum.com
Thank you so much for the code. I have been struggling with this for awhile...worked great!
Okay -- let's say I have 2 categories called "A" and "B". Some posts can be classified as both "A" AND "B". Some are "A" only and some are "B" only.
How would I work it to exclude posts from category "B" only but to still include them if they are categorized under both "A" AND "B"?
Awesome, worked perfectly. Thanks so much!
Sue, you ARE genius !!!!!!
Thank you SO MUCH for this really beatiful code... I was looking for it for a while cause my ancient version caused some bugs if home page is paged. Thank you once again.
A question :
are you using a filter to get a twitter ID input field before your comment text area? If so : please give that out in a blog post, please.
take care,
hi from france and russia
Hi Serge,
The Twitter field in comments is a plugin: http://fiddyp.co.uk/twitip-id-plugin-add-a-twitter-field-to-your-comment-form-easily/
Thanks
which I see is now unsupported (but still works okay)
Try this one
http://wordpress.org/extend/plugins/twitterlink-comments/
First I need to start using Twitter
(joking)
I'll try to see if there is a code to pull out of that plugin to see if it's hookable or filtrable without being a plugin...crazy a little.
Thanks for the link,
see ya
Sue,
This tip was a life saver for me. I spent several hours working with plug in's that didn't work the way they should, and then found your code which was an 'elegant' solution without the plugin 'bloat"
I'll be adding your RSS feed!!
Great post. It helps us out at our website Hpnotik Qrew.
Now post under Z Exceptional will not appear on our homepage.
Thanks for the codes. =)
Hpnotik Qrew
Hi there, please tell me if you know how if i can exclude a category based on the categorie's name not id. I`ve tried with -Category name but doesn't work. Or if i can pull the cateogry id from a specified category name. Thanks.
Hi. Can I exclude some child category from homepage? I have parent category named like "Month" and few child category "January", "March", "December". I want exclude only one or two child category but with this code I can't do this. This code exclude parent with child. Thanks
this code did much better! Thank you!
Hi there!
First, thanks for this excellent snippet, it's doing exactly what I want it to. However...
I'm using this line of code to exclude the cat. 'blog' on the frontpage, and only displaying 'podcast' categories. Perfect. But, when I go to the 'blog' page, the 'blog' category is also vacant, only displaying 'podcast' posts.
At the end of the day, what I'm looking to do is exclude the 'blog' category from the frontpage, and exclude the 'podcast' category from the 'blog' page. Is this possible with this code, or should I be looking for another method?
Any help is greatly appreciated!
Super mega script...thanks!! Easy and quick
I have a blog and in this blog the following categories music, pop, rap, indie fashion, fashion spreads, fashion shows, lifestly&culture, beauty, etc. however I want my blog to have the main categories fashion, music, lifestyle&culture, beauty post on different pages, but work just like the home page where the recent post in those categories always come up. How do I do this? is it even possible?
Thank you! This solution worked great on my blog at Best Date Night Ideas. Great post
Thank you so much!
dc
Thanks for this. If I'd found this instead of the first method I went with, it would have saved me a few hours of clean-up time...
I've been trying to figure out how to do this for ages. So glad I found this post. Thanks, it works perfectly.
Thanks for this. It was quick and easy, and worked like a charm. Sometimes trying to accomplish the simplest of tasks can turn out to be such a chore.
Thanks for this post. I landed here on my google search, though I've found the code you posted elsewhere. I needed to exclude a category AND it's child categories. Here's what works.
/* this code excludes all of the 'international (id = 20) posts AND CHILDREN */
function exclude_category($query) {
$child_cats = (array) get_term_children('20', 'category');
if ( $query->is_home ) {
$query->set('category__not_in',array_merge(array('20'), $child_cats));
return $query;
}
}
add_filter('pre_get_posts', 'exclude_category');
Thanks! Really worked!
I have tried this code on 2 different themes and both time I get the white screen of death as soon as I try to click update. Even after I remove the code by going back and re-updating or even manually through ftp the white screen of death continues. Not only does it continue but it affects the entire admin area. No login screen no pages available unless they are in browser cache. The only solution after long diagnostic tests was to delete the theme through ftp and let it revert to default. Re-installing the themes work fine until trying the code again. All the time the front end displays fine. The code itself works in excluding the category from the home page but in doing so renders the entire admin useless. Any thoughts?
Sorry I forgot to say that I am using Wordpress 3.0.1 and the themes were White as Milk and Fifty Fifth Street. Fifty Fifth Street theme is certified for Wordpress 3.
Hope that helps.