Control Your Content with query_posts
I’ve heard a lot of people online complain that while WordPress is easy to learn at a basic level it can be hard to unlock some of its more complex features. When I first was learning to make WordPress themes, I found myself stuck making only "blog style" themes. You know what I’m talking about: a basic page with a header, a navigation, a long section of posts, a sidebar, and a footer. I was relatively happy with them and even today that style still serves a definite purpose.
But sometimes you have more than one type of content to present. You may want a large, featured post area for showing off recent work. You may want more than just a sidebar recapping your recent posts, perhaps a sidebar with actual, fresh content in it. This post will hopefully shed a little light on an easy way to gain control of your WordPress content. Using “query_posts” to pass parameters to your WordPress loop will let you put any content any place in your layout.
I feel a quick warning is in order. This post is a little bit wordy and is intended for users who are already somewhat familiar with WordPress.
Below is the example I’m basing this article on (and it just so happens to by my personal blog). I’m sure there are many other great examples using query_posts but since I can’t see their code I chose this one to make sure it was a valid example.

A quick glance shows you that I have multiple types of content. I have a space to show recent work (it slowly fades between the three most recent entries in the category "templates"). I have a space to show my recent blog posts (it shows the ten most recent entries in the category "posts"). In the first sidebar, I have a section where I post short reviews of movies I’ve recently seen (shows the seven most recent posts in the category "movies"). In the second, smaller sidebar, I have a space for a mini design showcase (it shows the five most recent posts from the category "design").
Most themes use The Loop for getting the posts from the WordPress database. If you have made a WordPress theme, you are probably familiar with The Loop. Along with The Loop, query_posts is an extremely useful tool. Here is what WordPress.org’s codex has to say:
"Query_posts can be used to control which posts show up in The Loop. It accepts a variety of parameters in the same format as used in your URL (e.g. p=4 to show only post of ID number 4). "
Query_posts is placed right before The Loop and will change what posts The Loop retrieves. For example, if you wanted to only call posts from the category "movies" your query would look like this:
<?php query_posts('category_name=movies'); ?>
and you would place it right before your Loop. The whole thing would look like this:
<?php query_posts('category_name=movies'); ?>
<?php while (have_posts()) : the_post(); ?>
...do stuff...
<?php endwhile;?>
"…do stuff…" is of course where you would put your template code. Things like the_title and the_content and your layout. Now lets get more specific. Say, for example, if you wanted only the most recent five posts from the category "movies" that had the tag "comedy" you could use this query:
<?php query_posts('category_name=movies&tag=comedy&showposts=5'); ?>
You can tack on as many parameters as you need. This allows you to have complete control over any bit of content in your database. You can create layouts and not really have to worry about how you will get WordPress to stick each piece of content where you need it. It allows you to be more creative with your layouts and with your content. In my blog’s current layout, I would never want those mini movie reviews to show up as a regular post; they are far less significant. Displaying them in a smaller, sidebar sized area works much better for the layout.
Using this technique you can achieve very complex "magazine" style themes, CMS style themes, professional business themes not geared towards blogging, and anything else you can dream up. You can even use it to display single items. Say you had a client who wanted his contact info listed at the top of this theme. Its just a few lines of static information, right? Normally you would just insert this directly into one of the template files. Instead, using query_posts you could call up only a single post that is in a category "info" and is tagged "contact." The query would look like this:
<?php query_posts('category_name=info&tag=contact&showposts=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile;?>
Then if the client needed to change his contact info, he could just edit a post and not have to worry about digging into the templates. Calling posts in this way makes your entries extremely flexible. You could use the "info" category to hold all sorts of info: a short bio post, a introduction paragraph, a message for your footer, or a quote from a client. Then you could call those specific posts anywhere you’d need them. It really allows you to have an extra way to enter, store, and display content.
If you use query_posts around each Loop you use, you don’t have to worry about these extra posts showing up anywhere else. If you still are using a regular loop you may want to exclude the "info" category, and you could do so with a query like this:
<?php query_posts('cat=-3'); ?>
where "3" is the ID of the category "info." Notice there are many different category parameters (like category_name, cat, category__and, etc). For a complete list, read the query_posts page on the WordPress codex. While learning how to use query_posts you’ll want to keep this page open for reference and to see what its really capable of.
There is one down side, though, but its not a major one. When templating in this way you end up with certain category names, category IDs, post tags, etc hard coded right into your theme. If its a job for yourself or for a client, this is just fine (since you are the one setting up all the categories and posts anyway). But if you are aiming to use it in a template for resell, you’ll need to include full explanation and explain which categories need to be created and how to use them correctly make use of the theme. Of course, if you are more advanced, you could use a WordPress options page with your theme and let the user handle some of the setup. Dan Harper has an excellent write up about theme options page you can read here.
Once you’ve mastered query_posts and its parameters you’ll find templating with WordPress will be much, much easier. Your templates will be more flexible, dynamic, and organized. You’ll have complete control over every bit of content and will be able to switch it all out at a moment’s notice, without having to touch your template files. Its a great benefit to any theme for a client that might not be too savvy, but its also just a great way to setup your own blog or your next template for sale.


















http://codex.wordpress.org/Template_Tags/query_posts#Important_note
This was a great write up. I just recently created my first wordpress theme (see demo at http://youth.ndezigns.com) and shortly found out about the power query_posts. I’ll be using it quite a bit in the themes to come.
This is a really good article. One thing that is worth mentioning also with the query is to use the query reset if you are using Wordpress conditional tags for your menu and other stuff.
In my case, I include this code in the footer in order to avoid conditional tag to be wrongly assumed as true when using the query_post:
Thanks !
@Valentino.
It works though. Are there any better examples of the mentioned second method to use?
If you use this method : , then your posts’ order will automatic become ascending, even i include order=desc into the query. The only way to solve this issue is use WP_query.
Thank you for this. I needed this thing and very happy with founding it on In the Woods blog.
Cheers!
hi
i am trying to build a template which can be used many times. i am trying to use custom fields to define a category slug that can be used in query_posts but i am not able to get it working.
can you please suggest how i can achieve this.
thanks in advance.
As Valentino pointed out – this post_query can only be used once in the theme for the main loop. Other multiple loops must employ WP_query – this is at least what WordPress creators suggest.
thanks for the write-up.. Been awhile since ive played with this part and forgot how to use .
I have a question though.. am i going to run into a problem using query_post twice on my index page inside the loop? I have a featured section at the top and I want that section to only displays posts from a specific category, and then below that all the regular posts except the featured ones.
so far I dont see any problems and teh featured post is displayed fine and regular posts below but im worried it may cause issues later??
thanks
Hello, If I want to show the first post with post id = 181 and show 3 othes post. How I code the query_posts?
Hey to avoid the subtraction of categories (ie cat=-3) I’ve detaild how to remove them by name in my blog post here: http://bloggercamp.com/blogging-tips/blogging-tips-wordpress-tips/remove-several-categories-from-the-loop-in-wordpress/
wow i think i will need this. thinks this will help me build the index.php. Probably, show some category will be a nice touch. Thanks
This came is very useful, thanks!
Holy sh..
This is what I’ve been looking for.
For the last week, I’ve been trying to figure out how to display certain posts on the site I’m building, where I needed to have a “featured” post, a “news” box, and also some quick posts.
This explains everything.
Thank you thank you thank you
thanks a lot from a french guys that you really help !