Add Custom Loops (Queries) to WordPress

If you look at your WordPress template, the thing that drives your page is called “the loop.” That’s WordPress speak for looping through the result set (the posts) retrieved from the database.

If you wanted to make custom queries, you could certainly do that by diving straight into the database. However, WordPress comes with some built in functions for helping you query the database and display the results. By using these template tags, you can add custom loops in your template to display other post information.

Looking At the Main Loop

If you look in any of your templates, you’ll see a loop similar to the one below. This basic loop just loops through all the articles, outputs the title and content.

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<div><?php the_content(); ?></div>
<?php endwhile; ?>
<?php endif; ?>

WordPress has a nasty habit of using that weird while/if syntax, where things aren’t enclosed in brackets. Aside from the weird sytnax, this is a very simple while loop.

First, if (have_posts()) checks to see if there is any data in the queue to be outputted. If there is, it starts a while loop that uses the same check – have_posts() – to see if it should continue.

The thing that moves this along is the_post(). This loads the next post in the result set into memory and prepares it for outputting.

The next few lines do our basic output. the_title() is the template tag for outputting the title of the current article, while the_content() outputs the text of the article.

Finally, we wrap up the while loop and the if statement. If there were more articles in the queue (like on an index/archive page), it would loop through for each article.

Where Do the Articles Come From?

When you load a page in WordPress, you use a query string. The most basic one would be index.php?p=5. That tells WordPress to load the article where the post id is 5.

You may not realize it if you’re using pretty URLs, but every URL you request on your blog has one of these query strings built-in. It tells WordPress to load a list of articles (for an archive, category, or index) or to simply load one article or page.

There’s nothing stopping you from utilizing this process for other purposes, though. Rather than simply looping through the result-set created by the query string, you can use the query_posts() function to create your own custom queries – and then loop through them.

Example Please…?

Here’s how it works. You call query_posts() with a query string. That loads the database results and prepares for the loop. Then, you loop through like we did above to output information on the articles.

Let’s say you want to fetch the latest article that was tagged with both “foo” and “bar.” Here’s how you do it.

query_posts("tag=foo+bar&showposts=1");
//  Create the loop here

We passed two parameters here. “tag=foo+bar” tells WordPress to fetch articles that have both of those tags. “showposts=1″ tells WordPress to only fetch one article. By default, they’re sorted by date and time, so we automatically fetch the latest one.

You Don’t Always Need Plug-ins

This is such a great tool because it lets you avoid using some basic plug-ins. Plug-ins essentially help you perform these extra queries – like the latest posts, similar posts, etc. But they can also be restricting if they don’t do exactly what you want.

By using query_posts() to fetch your own information, you can get WordPress to do exactly what you want.

For example, let’s say you write movie reviews and you want to display the latest ones in your sidebar. Rather than look for a plug-in that displays exactly what you want it to, you can tag all of your movie reviews with “Movie Review” and query for those specific posts.

Then you can easily create the output you want, rather than the output someone else designed.

This is just a taste of what you can do with query_posts() and custom loops in WordPress. You’ll need to try it out for yourself to fully understand it, and I’d suggest you take a look at the documentation on query_posts() for a list of parameters and the documentation on template tags for a full list of output tags.

Good luck!

Bookmark and Share:
  • Digg
  • Furl
  • del.icio.us
  • StumbleUpon
  • MisterWong
  • DZone
  • Technorati

Tags: , , ,

3 Comments to “Add Custom Loops (Queries) to WordPress”

  1. Oscilloscope said this on

    i can see a lot of movie reviews on the internet and i usually buy dvd movies with great reviews ”.

  2. uprss said this on

    Reader CNET Reviews -Most Recent Handheld

  3. Dorthey Privitera said this on

    I am not sure where you are getting your info, but great topic. I needs to spend some time learning much more or understanding more. Thanks for excellent information I was looking for this info for my mission.

Leave a Reply