Excluding categories with query_posts()

Posted January 7th, 2009 by Jay

The wordpress theme I am using is WP-Pixels from Peter From.  I’ve always wanted a hybrid website and I found one that is totally easy to modify finally.  Anyhow I have loved everything about this theme except for the fact that when I use the blog template (blogger.php) for the blog, the template displays all category posts. I didn’t want that seeing as my features and portfolio should be separate.

So I’m guessing you’re wondering what I did, it’s quite simple really.  Just so you know, excluding any category from your “blog” does not render it in-accessible.  It can still be accessed and viewed by other means, in my case a category link at the near top of my pages.

First we need to go to the category page in the wp-admin panel.  Hover over the category you want to exclude from your blog and look at the bottom of your screen for the following highlighted item:
img-wp-query_posts

Now that we have the category ID we wish to exclude in our blog postings, we need to go to the blogger.php template file.  So go to Appearance->Editor and find the theme file you wish to edit, in my case it is the blogger.php file.  Some of you may wish to exclude something else from a specific page such as the index.php (home page) or otherwise.  None the less we need to find the query_posts() function and do a little editing.  Luckily the function is in the top of the file, we need to get a hold of the following:

<?php query_posts(''); ?>

and change it to

<?php query_posts("cat=-1,-6,-3"); ?>

What we’re doing is telling wordpress to negate or omit categories 1, 6, and 3 from the query_posts() function used in this file.  This is very hand in my case that I want to prevent image posts from showing in my actual blog.  You may have another use but that’s up to you.

Have fun coding - Jay

2 Responses to “Excluding categories with query_posts()”

  1. assaulseW Says:

    Hello. Your site displays incorrectly in Opera, but content excellent! Thank you for your wise words:)

  2. Jay Says:

    Thanks for the update on the Opera issue. I don’t see me fixing it in the near future, but if I do go for some optimization I’ll definitely take heed. Thanks for the kind word :-)

Leave a Reply