Excluding certain categories from your blog main page
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.
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.

October 3rd, 2007 at 5:59 am
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
October 3rd, 2007 at 9:07 am
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.
October 3rd, 2007 at 10:36 am
hi, i have emailed you. hope u’d help.
October 5th, 2007 at 9:31 pm
[...] to generate traffic for your blog. I wrote some tech tips on WordPress customization: How to exclude certain categories from your main page and how to change your permalink structure without breaking the old [...]
November 25th, 2007 at 2:09 pm
to exclude multi cats just use for example
November 25th, 2007 at 2:10 pm
sorry, not sure how to post the code here lol
[code][/code]
November 25th, 2007 at 2:11 pm
php query_posts($query_string . “cat=-5,-6,-4,-7″); ?
January 26th, 2008 at 11:01 am
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!!!
March 30th, 2008 at 6:03 am
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!
March 30th, 2008 at 12:51 pm
@Joel, you are most welcome
April 17th, 2008 at 12:26 am
great post, just what i was looking for, thanks homie!
June 6th, 2008 at 11:42 pm
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.
June 9th, 2008 at 10:20 am
Thanks Meitar for the tip…
July 19th, 2008 at 3:05 am
[...] Good has a nice post about the 2 methods you could use here, but I’m just going to breifly go over the method I use today. First, you’ll need to [...]
July 19th, 2008 at 3:32 am
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
July 19th, 2008 at 10:49 am
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.
August 26th, 2008 at 6:56 pm
[...] i googled this trick. just pop in <?php query_posts($query_string . “&cat=-23″); [...]