Using WP_Query() to make custom queries within WordPress templates
Well I was going to change to blog as I mentioned a while ago, but I’ve been very busy and haven’t had time yet. Keep a look out after Christmas though and things should start to change then. Until then though here is a little tutorial for all you WordPress users. Using WordPress is relatively [...]
Well I was going to change to blog as I mentioned a while ago, but I’ve been very busy and haven’t had time yet. Keep a look out after Christmas though and things should start to change then. Until then though here is a little tutorial for all you WordPress users.

This updated posts slider is a great example of what WP_Query can do.
query_posts(), which is fine, but if you want a featured post set, or a little updated posts section (picture on the right) you will want to use WP_Query().
WP_Query(); is used to create a new query that is independant of WordPress’ normal query, this means you can show the posts as normal & have another feature showing updated posts as I have shown you above. First let look at how to create a new instance of WP_Query().
$wp_query = new WP_Query();
The problem here is that WordPress’ normal query is also held inside the variable $wp_query. You would automatically think that using a different variable would work, well it won’t. If another loop is still held as active inside $wp_query it stops certain commands, such as the_tags() from working. So instead we do a little switching. Like this:
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('');
You can use any attribute that you would use in query_posts(). You can find all about query_posts() at the WordPress Codex. You might know all this, but something that most people don’t know is that if you use a plugin that needs to use the $posts array it won’t be poplulated with the correct posts. It will actually be populated with the posts from the main WordPress loop. To solve this, you need to re-set the $posts array. You can do it like this:
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=5');
$temp_posts = $posts;
$posts = null;
$posts = $wp_query->posts;
Then any plugins that need to use the $posts array will work fine.
That’s it. I hope that helps you with how to make custom queries in WordPress. Any problems then just drop me a comment. If you want to see an example of what custom queries can do then visit Purrfect Gifts @ Zazzle.

Discussion: 35 Comments » add a comment
I get what you are saying. It is just that I am reading a lot of conflicting things. Like I don’t understand why you want to delete the two temp thing at the top. I can easily wait until morning if you want to come back and explain. What I am trying to do now is just get the best non conflicting custom loop going so that it won’t break any plugins and whatever else in the wordpress theme I am working on.
Well using WP Query is the best way to get an independant loop that shouldn’t interfere with other loops.
Your code looks like this:
<?php $temp = $wp_query; $wp_query = null; $home_posts = new WP_Query(); $wp_query->query(array( 'category_name' => 'homepage','order' => 'ASC','posts_per_page' => 4 )); while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> <?php endwhile; ?>You have a sort of mix of two different loops here. I, simply because I’m used to is, use the method of copying the main loop, overwrite it, and then restore it. However there is an easier way of doing it. You could should work like this:
<?php $home_posts = new WP_Query(); $home_posts->query(array( 'category_name' => 'homepage','order' => 'ASC','posts_per_page' => 4 )); while ( $home_posts->have_posts() ) : $home_posts->the_post(); ?> <?php endwhile; ?>The explanation is something like this. During a normal loop the functions
query(),have_posts()&the_post()are aliased to the standard$wp_queryvariable which is always created by WP for the main loop. You want a custom loop, so you must call the functions created by your newWP_Query()object using OOP syntax.I hope that makes sense. It’s a little difficult to describe, lol. Let me know if there is something you are confused about & I’ll try and explain it further.
Hey there! I’ve been following your weblog for a long time now and finally got the courage to go ahead and give you a shout out from Porter Tx! Just wanted to say keep up the great job!