Excluding certain categories from your blog main page

Wordpress Logo This post only applies to people who are running a WordPress blog and have access to editing their template pages. A few days ago, I had a requirement where I wanted to prevent posts in certain categories from showing up on my blog’s main index page. There are a couple of ways mentioned on the WordPress Codex for this. However, try as I may, I couldn’t get them to work after several attempts (till I read the documentation very carefully). If you are facing the same problem, read on.

Note: The following require making changes to the templates files of your blog and you can potentially make your blog unreadable. Please proceed at your own risk. It is imperative to take a backup of all the template files so that if you mess up, you can restore the backup.

For the purpose of this article, I assume that you know how to edit your template file and have a basic understanding of The Loop in WordPress. Just follow the link to refresh your memories.

Method 1: in_category() function

There are two ways through which you can achieve excluding a category from your main index page. If you followed the link for The Loop and read through to the end of the page, you already know the first one. Within The Loop, you can simply add a line of PHP code which looks something like this:

<?php if (in_category(‘3′)) continue; ?>

By inserting this call, you are essentially telling WordPress to skip over displaying any posts which are in the category with CategoryID 3. This worked for me the first time, but there was a problem with it.

If you read the page carefully, then it is mentioned there that even though we skip over the post within a certain category, it still gets counted. So, in your settings if you have defined the maximum number of posts to be displayed as 10, and in your last 5 posts to your blog, 4 belong to the category you want to exclude, then your main index page will only display 6 posts.

Method 2: query_posts() function

This was not acceptable to me, so I looked at the other documented method of excluding a category from my Main Index page: using the Query_posts function. This function, when called before The Loop, creates a new set of posts for The Loop to work on. While there are many fun things that you can do with this function, our interest is in excluding categories. To exclude the category with Category ID 3, we should call this function as shown before The Loop begins:

<?php query_posts($query_string . “&cat=-3″); ?>

This allows you to exclude the undesired categories and yet be able to display the desired number of posts. Even this method is prone to a problem. This only works for posts which ONLY have category 3 (in above example). So, if you have a post which is filed under category 3 as well as 4, it will still show up. This I can live with; I don’t file the posts I want to exclude under more than one category.

There is one gotcha with this method. It doesn’t work with the old version of The Loop. Yes, if you were a careful reader, you would have noticed that there are a couple of versions of The Loop shown in the Codex articles. For the query_posts method to work, the beginning of your Loop must look like this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

This was my main problem. I was using an old theme and it had the old method of iterating The Loop.

Winding Up

Of course, these changes have to be made to all the template files in your theme which use The Loop and where you don’t want to show the posts within the concerned category; except the Category Page template of course, because you want to at least be able to access the excluded posts from their category pages.

If you found this content helpful, then please help by linking to me. You can also help me by sharing the content using any of these nifty buttons above. Thank you.



You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

39 Responses to “Excluding certain categories from your blog main page”

  1. hi, i tried your tip on the , do i have to place it before the loop begins in the index page? i tried, but at the end my website gone blank. please help me :(

    Reply
  2. Hi Bearthepooh,

    This is to be placed before the line that says:
    < ?php if (have_posts()) : ?>

    You can email me your index file and I can look at it.

    Also, I am assuming you are using the Second Method.

    Cheers.

    Reply
  3. hi, i have emailed you. hope u’d help. :D

    Reply
  4.   Mike Honcho Says:

    to exclude multi cats just use for example

    Reply
  5.   Mike Honcho Says:

    sorry, not sure how to post the code here lol

    [code][/code]

    Reply
  6.   Mike Honcho Says:

    php query_posts($query_string . “cat=-5,-6,-4,-7″); ?

    Reply
  7. I like Method 2 but I can’t get it to work right… I am using WP 2.3.2 and every method I’ve tried so far (except method 1) reorders my posts display my very first post at the beginning of my blog and the newest one somewhere in the back…

    I’ve tried finding answers on the WP board but that’s just too unorganized, imo.

    Any ideas?

    Thanks!!!

    Reply
  8. This was EXACTLY what I was looking for, because the recommended technique of calling in_category() threw off the counting, as you point out. Thanks very much!

    Reply
  9. @Joel, you are most welcome :)

    Reply
  10. great post, just what i was looking for, thanks homie!

    Reply
  11. Sometimes simply excluding a whole category is too broad, and other times the query_posts() function doesn’t provide you with the hook you need. In these cases, you can run the original query, note any modifications you need to make, and then create new, modified query and display the results from that one instead. For instance, if you need to exclude posts based on category and, say, the beginning of their title.

    I wrote instructions with sample code on my own blog about how to do this.

    Reply
  12. Thanks Meitar for the tip…

    Reply
  13. Hi
    I’d just like to point out that there’s a problem with the function you provided for method 2. The correct function should be:
    <?php if (is_home()) {
    query_posts($query_string . "&cat=-3");
    }
    ?>

    Cheers :)

    Reply
  14. Hi Mimi, the function that I described works as well. All you have to do is to make sure that you place it on your index.php.

    Reply
  15. Hello,

    Thank you very much for this code. I have implemented it on my blog and it works great. But I was also trying to do something else and I cannot figure it out on my own. Could you please tell me how to do the following:

    I would like to include certain posts only from the home page. In other words, the post should show up in a certain category, but not on the home page. I cannot figure out how to do this without affecting the number of posts that is shown on the home page, like you did with Method 2 for a category.

    Thank you.

    Reply
  16. I am sorry, I made a mistake. Instead of “I would like to include certain posts…” it should read “I would like to exclude certain posts…”

    Reply
  17. @Eugene – just create another category (something like nothome) and mark these posts in that category.

    And only on your homepage, filter out the results for this category using the same code as above.

    Reply
  18. Vaibhav,

    Thank you for your answer. I have thought of that, but that category has a good page rank, so I would to be able to keep putting posts in that category, it is just that sometimes I need them to skip the home page.

    Reply
  19. Right. Well, theoretically, you can change the query however you like. I have not done that in the past.

    You could have a special tag or a custom field which you can fill out for posts which you want to include. And then you can create a custom query which checks for this tag or custom field and excludes them from the query results.

    Reply
  20. Thanks for your post! I used the info provided here, and I was able to exclude a certain category from the home page excerpts. I tried using the Wordpress Codex database, but it didn’t help.

    Reply
  21. Hi

    I’m trying to get this to work but I’m stuck.
    My loop is different than most. I have a simple blog layout, not magazine style and I’m trying to exclude posts from a certain category. I have tried but get an error. I tried the code from one of the commentators with no error, but also with no result.

    This is my loop:

    have_posts()) : $top_query->the_post(); ?>

    Why doesn’t your code work for me? I have placed it before the code above.

    I’m running WP 2.7.

    Thanks in advance!
    Ryan

    Reply
  22. Oh hell it seems my previous post doesn’t show the entire code??

    I’m going to replace to open and close brackets:

    (?php if (have_posts()) : $top_query = new WP_Query($query_string.”&cat=-”.get_option(‘ds_featured’).”,$cat”); while($top_query->have_posts()) : $top_query->the_post(); ?)

    Reply
    • Hi Ryan, I am not so sure on why it is not working (I have a migraine right now, so I might just be brain dead)…

      Have you tried hard coding the category id that you are trying to exclude (just so that we know there is nothing wrong with the .get_option() function?

      Reply
  23. Hey, I’m posting your code before my loop starts but nothing happens… Can you explain why the category I specify isnt being excluded? here is my code:

    [code]

    [/code]

    Reply
  24. whoops, code didnt post:

    Reply
  25. ?php if (is_home()) {
    query_posts($query_string . “&cat=-40″);
    }
    ?
    ?php if (have_posts()) : while (have_posts()) : the_post(); ?

    Reply
  26. Hi Vaibhav

    This is what I have put together sofar:
    [code] [/code]

    Exclude posts in cat 4 from frontpage, but not if the post has more than one category to it.

    Do you have any suggestions/advice?

    Thanks!

    Reply
  27. Thank you so much!!! :) I tried several methods, including a plugin or two, and nothing worked. Your Method 1 worked perfectly, and took less than a minute to implement.

    Reply
  28. I found that method two worked for me, but only after I changed the double quote marks to single ones,like this:
    <?php query_posts($query_string . ‘&cat=-53′); ?> as the codex said here: http://codex.wordpress.org/The_Loop And hey, the drawback mentioned in the article for method 2 where the posts will be displayed anyway if they are also in another category did not affect my blog. They were excluded if they were in my excluded category even if they were in another category as well, which is fine for what I want! I am using a modified redline theme made from wp-framework and I’m running the newest WP as of now, 2.9.1. Thanks for the great post!

    Reply




Leave a Reply