Exclude a category from your WordPress index page

Do you want to exclude a category from your WordPress index page? It sounds like a simple thing to do, but there is a huge mess of blog posts out there with all sorts of crazy solutions. In this post I will try to simplify that mess by providing 3 simple ways to exclude a certain category from your WordPress blog’s homepage. Each method involves inserting a small snippet of code into your index.php file, you don’t need any plugins or other files.

Method 1: Use the in_category() function

This is a single line of code that very simply causes your homepage to skip over any posts that are in a certain category. Lets say that you want to exclude all posts from the category with an ID of 8 from your WordPress index page. Simply place the following code into your index.php file after this line <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

&lt;?php if (in_category('8')) continue; ?&gt;

Issues with this method:

Even though your posts in a certain category are skipped over, they are still counted in your post count. So it won’t be possible to always display a fixed number of posts on your homepage. In the worst case, if you are displaying 10 posts on your homepage and your latest 10 posts are in your excluded category, then you will have no posts displayed on your homepage.

Method 2: Use query_posts() and pagination

These two lines of code basically use the WordPress query_posts() function to exclude posts in a certain category. The first line of code fixes the pagination issue of all posts being displayed on all pages. Again, let’s say that we want to exclude all posts from the category with an ID of 8 from our index page. Simply place the following code in your index.php file before this line <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

&lt;?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=-8&amp;paged=$paged"); ?&gt;

Issues with this method:

This method will remove posts that are ONLY in the excluded category. If a post is in the excluded category as well as another category they will still show up on the home page.

Method 3: Another query_posts() method

Again we use the query_posts() function to exclude posts in a certain category. As with the above method, let’s exclude the category with the ID of 8 from our main page. Simply place the following code in your index.php file before this line <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

&lt;?php query_posts($query_string . '&amp;cat=-8'); ?&gt;

Issues with this method:

Like the above method, this method will remove posts that are ONLY in the excluded category. If a post is in the excluded category as well as another category they will still show up on the home page.

Edit 15.11.2010: This third method will actually remove posts that are in the targeted category as well as other categories too. So basically any post that is in the targeted category will be removed even if it’s part of other categories.

Hope that helped!

The index.php file can be found in the root folder of your theme or you can simply edit it online using the WordPress Theme Editor which can be found in your WordPress admin panel under Appearance -> Editor -> index.php. Hopefully this has cleared up some of the confusion regarding excluding a certain category from your wordpress home page. Please let me know if anyone has any other methods of achieving this.

Adham Dannaway

Web Designer and Front End Developer with a passion for web design, coding, blogging, WordPress and chewing gum. Check out my web design portfolio and follow me on Twitter and Facebook!

Tags: , , , , ,

  1. Niall Doherty on September 12, 2009

    You sir, are a legend.

    Thanks very much for this. I found the first method described in many other places, but ran into trouble when I tried to exclude 6 new posts from a homepage that only shows 5 posts. The result was a blank homepage.

    The two other methods you have here worked like a charm though. I’m running with the last one.

    Thanks again.

  2. Cre8ive Commando on September 13, 2009

    Thanks Niall. There seems to be a lot of confusion regarding excluding categories from WP index pages. Glad I could help out. 🙂

  3. Topper on October 9, 2009

    In Method one… Could you have something like “Postcount=postcount-1” before the continue, so that the number of posts on your front page doesn’t get effected when passing over a post?

    Sorry I don’t know the correct coding or language for it. But I think something like that could be added.

  4. Jayson on November 8, 2009

    Thanks for clarifying!

  5. donna on January 25, 2010

    Worked like a charm – and easy to understand! Thanks bunches.

  6. Marquita Bischke on March 13, 2010

    This is a great blog you got here. The theme looks nice, awesome color combination.

  7. Cre8ive Commando on March 14, 2010

    Thanks. Glad you like it 🙂

  8. Jordi on March 30, 2010

    I just tested method 3, and it does not have the limitation that you advertise. I mean, articles which belong to the banned category and other categories are still “banned” in the home page.
    Is this being experienced by anyone else?

  9. One Royal Way | Travis on May 28, 2010


    I am having the same issue. I am wanting to exclude posts that are ONLY in a certain category. For example, I have a Kansas City Royals blog. Each day, the starting lineup is automatically posted.

    Category: Royals Lineup

    However, on days that I am sitting at home, ready to watch the game, I also add a “What will happen in tonight’s game” poll.

    Categories: Royals Lineup AND Royals Polls

    I do not want to include the lineup on the homepage on days when I do not add a poll, but if the post includes a poll, I want to include it on the homepage. If you know how to accomplish this, please send me a message:



  10. Tipografo on August 20, 2010

    wow, the first tip is delicious. thanks, just used it in my latest theme.

  11. TheFunFamilyMom on November 14, 2010

    Great information! I am trying to do just this, but before attempting one of your methods above I would like clarification on one thing:

    My template (Amazing Grace) came with only an index.php file. There is no category.php file. To make the above method work and still show posts in category 8 when readers choose the category, do I need to create a separate category.php file (by copying the index.php) and then make the above suggested changes ONLY to the index.php? Thanks. Hope my question makes sense.

    If you look at my website, I am trying to make a new category that will not display on the home page.

  12. thefunfamilymom on November 15, 2010

    Couldn’t wait for response but decided to copy my index.php file and create a category.php file. Then used option 3 from above. Worked like a charm! Thanks for the simple solution.

  13. Cre8ive Commando on November 16, 2010

    @thefunfamilymom There should be a file called archive.php. This is the file that handles the display of your WordPress categories on your website. So category 8 will be hidden from your homepage post list but you will still be able to see it by going to the category page: e.g. site.com/category/8

  14. Reg on January 19, 2011

    Thank you! For a designer with no coding/scripting experience, this was very helpful. I used method 2 to exclude all but one category from my index page. Probably not the best practice (excluding multiple categories instead of displaying just one category) but the important thing is, it worked (and so does my page nav). Thanks!

  15. eren on February 26, 2011

    thank u. it is very great post. usefull…

  16. Karin on March 19, 2011

    both for the different ways and for point out where to put the code

  17. Georg on April 7, 2011

    Thank you so much! I read so many forum posts that this wouldn’t work etc.
    I found one solution that would loop the same results page over and over, when flipping pages.
    Thanks for this one little line of code that solved this problem!


  18. Rodger - Blog e-commerce on April 27, 2011

    Hi and thank you for all this tips very usefull. I need to exclude a category from my home page on the loop. Can i do this in my index.php file :

    $lastposts = get_posts(‘showposts=8&cat=-cat_name’);

    Tank you for your help.

  19. rafael on May 4, 2011

    how do I exclude category from a search result?

  20. David Dunford on September 6, 2011

    Thanks for the tip! Found some other plugins however they’re not working.
    Simple solution without another plugin sounds great to me.

    Using #3 and works like a charm!
    Cheers 😉

  21. Katie @ StylishandTrendy on July 11, 2012

    Method 2 is better as it doesn’t break the pagination. Other two methods usually ignore paged variable which causes pagination to break.

  22. hcd on May 30, 2013

    Great job! Very useful for a class that I am teaching, method 3 worked best for me by far.


  1. […] link: Exclude a category from your WordPress index page Comments0 Leave a Reply Click here to cancel […]

  2. […] 1. cre8ivecommando.com 2. hameedullah.com Etiketler: in_category(), pre_get_posts, query_posts(), WordPress Yorum yok […]

  3. […] Exclude a category from your WordPress index page | Cre8ive Commando.   If you enjoyed this article, please consider sharing […]