The Power of WP_Query

halo wordpress 400x310 The Power of WP Query photo

WP_Query is a powerful tool to control what to display on your blog. This is very useful for plugin and theme designer, especially for magazine style themes which need to display posts of different categories in different sections. In this post, I will teach you how and when to use WP_Query.

Case 1: Display posts in particular category

In some WordPress themes, we want to show some posts in particular category(e.g. Featured Posts), the easiest way to do this is to use WP_Query. Here is the example code:

<?php
    $FeaturedPosts = new WP_Query();
    $FeaturedPosts->query('category_name=Featured&showposts=5');
    while($FeaturedPosts->have_posts()) : $FeaturedPosts->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark">< ?php the_title(); ?></a></li>

<?php endwhile; ?>

I defined a variable named $FeaturedPosts and instantiated an instance of WP_Query. I then used a method of WP_Query to start a query to get 5 posts in the category named Featured. The last step is to start the loop to display the post, Here I only showed titles of posts.

Now you understand how WP_Query works, let’s see the next case.

Case 2: Don’t display posts in particular category

This is a opposite case to Case 1, let’s cut to code:

<?php
    $FeaturedPosts = new WP_Query();
    $FeaturedPosts->query('cat=-1&showposts=5');
    while($FeaturedPosts->have_posts()) : $FeaturedPosts->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>

<?php endwhile; ?>

You can see the difference between these two cases is in the Query function. I use ‘cat=-1′ to prevent the first category to be displayed.

Case 3: Display certain posts in one page

In this case, I am going to override the page navigation setting. Here is the code:

<?php
    $FeaturedPosts = new WP_Query();
    $FeaturedPosts->query('posts_per_page=5&offset=1');
    while($FeaturedPosts->have_posts()) : $FeaturedPosts->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>

<?php endwhile; ?>

posts_per_page sets how many posts to be showed in a page, offset=1 means don’t show the first post, start from the second post.

Case 4: Display recent posts

There are a lot ways to display recent posts, WP_Query can do this as well. Just use the ‘showposts=5‘ as query term, it will return the recent 5 posts.

$FeaturedPosts->query('showposts=5');

WP_Query can do a lot of thing, here I only show some common cases, you can use your imagination to expand them. You can also check out the WordPress codex for WP_Query..


Feel free to visit our Forum, subscribe our RRS Feed for news updates, and follow us on Twitter.

Most Popular Posts

This entry was posted in Plugins, Tutorials and Hacks and tagged , . Bookmark the permalink.

2 Responses to The Power of WP_Query

  1. James Jack says:

    Your code has not been tested? “Featured” must be “FeaturedPosts”.

  2. Zack says:

    @James
    You are right, I was using $Featured at the beginning, and then change it to $FeaturedPosts, and forget to change the ones in the loop, Thanks James, I’ve fixed it now.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>