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) {

Share this post:
  • email
  • del.icio.us
  • Facebook
  • FriendFeed
  • LinkedIn
  • StumbleUpon
  • Tumblr
  • Twitter

Tags: , ,

Posted by Sue on April 17, 2009 in WordPress.

45 comments to "How to exclude categories from the home page of your WordPress blog"

  1. Sue (@blogmum) wrote:

    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. :-)

  2. 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 :-)

  3. Sue (@blogmum) wrote:

    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.

  4. 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

  5. I want to say big thanks to you .

    your code made my life easier .

    thanks

    Adnan

  6. This was awesome. Worked perfect and was easy. I tried 5 different plugins before finding this post, and none of them worked.

  7. Sean McGilvray wrote:

    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 :)

    • Sue (@blogmum) wrote:

      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');

  8. Nigel wrote:

    Hi

    Where in functions.php should this code be placed. Right t the top?

  9. Sue (@blogmum) wrote:

    Hi Nigel:

    Immediately after the first <?php is probably the safest place to paste it.

  10. Marc b wrote:

    Where is functions.php? I'm using a custom template.

  11. Sue (@blogmum) wrote:

    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.

  12. 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.

  13. Jason (@stocknerd) wrote:

    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');

  14. Jason wrote:

    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.

  15. Brad wrote:

    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

    • Sue (@blogmum) wrote:

      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

  16. Rick wrote:

    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!!

  17. Giuseppe wrote:

    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

    • Sue wrote:

      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!

  18. Giuseppe wrote:

    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

    • Sue wrote:

      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.

  19. Giuseppe wrote:

    Sue,
    thanks, but after various attempts, nothing to do for me.
    I'm going to apply more time on the codex.

    ciao
    Giuseppe

  20. Giuseppe wrote:

    Sue,
    i'll keep you in mind if i want to do it so.

    ciao

  21. Steve wrote:

    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

    • Sue wrote:

      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?

  22. Steve wrote:

    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.

    • Sue wrote:

      Are you kidding, that's a superb picture! :-D

      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.

  23. Steve wrote:

    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.

  24. Jeff wrote:

    Thank you so much for the code. I have been struggling with this for awhile...worked great!

  25. Liam wrote:

    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"?

  26. Jess wrote:

    Awesome, worked perfectly. Thanks so much!

  27. serge wrote:

    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

  28. serge wrote:

    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

  29. Gerry wrote:

    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!!

Leave a Reply