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.
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: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.
January 14th, 2009 at 12:21 pm
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.
January 14th, 2009 at 12:23 pm
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…”
January 14th, 2009 at 12:33 pm
@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.
January 14th, 2009 at 1:35 pm
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.
January 14th, 2009 at 8:47 pm
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.
February 26th, 2009 at 5:19 am
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.
April 19th, 2009 at 11:41 pm
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
April 19th, 2009 at 11:45 pm
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(); ?)
April 20th, 2009 at 11:24 am
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?
April 30th, 2009 at 10:22 pm
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]
April 30th, 2009 at 10:22 pm
whoops, code didnt post:
April 30th, 2009 at 10:23 pm
?php if (is_home()) {
query_posts($query_string . “&cat=-40″);
}
?
?php if (have_posts()) : while (have_posts()) : the_post(); ?
May 1st, 2009 at 12:20 am
Hi TBG,
Can you try this outside the is_home() function? I am not sure why this shouldn’t work.
Cheers.
July 11th, 2009 at 3:24 am
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!
July 11th, 2009 at 3:25 am
Sorry, code didn’t get through…
July 11th, 2009 at 3:26 am
Oh, boy… big apologies:
?php if(!is_category(array(1,2,3)) {query_posts(‘cat=-4′);} ?
July 11th, 2009 at 3:28 am
Dropped you an email
July 14th, 2009 at 11:52 am
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.
July 14th, 2009 at 11:59 am
You are most welcome of course.
July 14th, 2009 at 12:20 pm
Oops, I meant to say “Method 2,” since I also found the display gaps unacceptable. Again, thank you!
January 22nd, 2010 at 5:12 pm
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!