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(); ?>

<?php if (in_category('8')) continue; ?>

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(); ?>

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 query_posts("cat=-8&paged=$paged"); ?>

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(); ?>

<?php query_posts($query_string . '&cat=-8'); ?>

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.

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

Yep, that's me. I'm a Web Designer and Blogger from Sydney Australia with a passion for noodles, beer and design. Check out my other sites: Mac or PC, illumin8 Design

Follow me on Twitter »

Tags: , , , , ,

  • Stumbleupon Cre8ive Commando
  • Delicious
    • Share/Bookmark

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

  2. Niall Doherty on Tuesday 25, 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.

  3. Cre8ive Commando on Tuesday 25, 2009

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

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

  5. Topper on Tuesday 25, 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.

  6. Jayson on Tuesday 25, 2009

    Thanks for clarifying!

  7. donna on Tuesday 25, 2009

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


Subscribe to Cre8ive Commando RSS
Follow Cre8ive Commando on Twitter