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.

Celeb O Rama

This updated posts slider is a great example of what WP_Query can do.

Using WordPress is relatively simple, but if you delve into making templates it can get confusing. One of the things most people find confusing is how to use custom queries. Most people will tell you to use 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.