Display Certain Categories Within Wordpress

If you want to display a Wordpress front page in a new or interesting way by splitting the categories into sections, or by not displaying certain categories at all then you can use the query_posts() function. This function comes as part of Wordpress and allows you to override the queries that are being executed behind the scenes. This basically controls what posts are seen by "the loop". In order for the function to work it must be called before "the loop", look out for this line (or similar):

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

And put the call to query_posts() before that. You will need to give it certain parameters in order to do something.

So what can you do with this function? Well all sorts of stuff, but for this post we are just interested in getting different categories out, so lets concentrate on that.

With regards to parameters the function takes the following arguments. Notice that the last three have a double underscore.

  • cat
  • category_name
  • category__and
  • category__in
  • category__not_in

The most basic thing you can do is pass the category ID with the parameter cat. The following bit of code will get those posts in category 3.


You can do this the other way around as well by adding a - to the category ID and therefore getting everything but that category.


You can pass multiple category ID's using commas. The following code will get categories 2,3 and 8.


Excluding categories can be done in the same way.


Using category ID's can be a bit confusing, but luckily you can also pass the category name. Note that you can't include multiple categories this way, so this is only useful if you want a single category to be displayed.


To display posts that appear in multiple categories you can using the category__and parameter. The code here will display posts that appear in category 3, category 8 AND category 9. You can add as many as you like to this list.

query_posts(array("category__and" => array(2,8,9)));

The category__in parameter works in just the same way as a comma separated list used with the cat parameter, it is just a different way of doing the same thing. The following code will get posts in categories 1, 2 and 3.

query_posts(array("category__in" => array(1,2,3)));

The category__not_in parameter works in the opposite way from the category__in parameter. The following code will get posts that are not in categories 5 and 6.

query_posts(array("category__not_in" => array(5,6)));

It is also possible to change the order that that posts are printed out in. The default is to have the posts printed out in descending order based on the published date. To change this you can just add in the parameter order followed by ASC to make the posts sort in ascending order.


You can explicitly set the descending order by using the DESC parameter argument.


Try a few combinations yourself. It is also possible to have different sections by calling the query_posts() function a second time, just remember that you need another loop in order to print out the posts. To simplify things your can split out the loop into a separate file (I called mine post.php) and include it will the following snippet.

<?php include (TEMPLATEPATH . "/post.php"); ?>

You can then write query_posts(), include(), query_posts(), include(), query_posts(), include().


Thanks a lot for this useful post. It helped me a lot with my blog.

Add new comment

The content of this field is kept private and will not be shown publicly.
3 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.