1. Default Loop
  2. Loop with get_posts()
  3. Customize the Loop with pre_get_posts
  4. Loop with query_posts()
  5. Loop with WP_Query()
  6. 30-Second Summary

Each of these looping methods is useful in a variety of situations. They share a lot of the same underlying functionality, and the query parameters are essentially the same. Collectively, these four techniques enable simple loops, multiple loops, and custom loops in your WordPress theme template.

A good place to find a default loop, for example, is in your theme’s index.php file. Its purpose is to loop through the posts stored in the database and echo their contents to the browser. Using WordPress’ template tags, it’s easy to display post titles, content, meta info, and much more. That said, let’s examine the four ways to loop with WordPress.

The Default Loop

The default WordPress loop looks something like this:

What makes it “default”? Mostly because it uses the default query to loop through post content, making it the loop used like 99% of the time for most themes. It tells WordPress to loop through posts and display the information according to context, and as called by the various template tags (the_titlethe_content, et al). There are tags available for just about any type of data stored in the database.

Based on the query that is sent, the default loop will display a certain number of posts from a certain category from a certain date.

So the default loop is perfect if you’re happy with the query that is sent, but it is also possible to customize the query and generate a different set of posts.

Loop with query_posts()

Let’s say you have a default loop in your index.php theme template, but you want to change the number of posts, exclude two categories, and display the results in ascending order. Easy. Just add some query_posts action before the default loop and wp_reset_query immediately after, like this:

When to use it?

Use query_posts to modify the type of posts that are returned for a single loop. It’s perfect for limiting the number of posts, excluding posts from a certain category, and so on. If more than one loop is required, multiple query_posts loops could work, but there is a much better way to do it using WP_Query.

Loop with WP_Query()

WP_Query is a way to go for complete control over customisation of any number of loops.

Lets see an example below:

It also accepts the same parameters as query_posts, so modifying stuff like number of posts, included/excluded categories, and post order looks quite familiar. WP_Query makes it easy to customize the loop by simply changing the parameter:

With WP_Query, we don’t need the $query_string variable

When to use?

Use WP_Query for creating multiple, customised loops. By setting up additional instances of WP_Query in your theme, you can create any number of multiple loops, and customise the output of each.

Loop with get_posts()

One of the easiest and the safest way to create multiple loops in your theme is to use get_posts(). Anywhere you need to display a quick, static set of posts, get_posts is the perfect choice.

Here’s an example:

This code creates a loop of all posts except those in the excluded category. By using any of the same parameters accepted by WP_Query and query_posts, it’s possible to create loops that display just about anything.

When to use?

Use the get_posts() function to easily create additional, static loops anywhere in your theme. get_posts accepts the same parameters as query_posts, and is perfect for adding static, custom loops to your sidebar, footer, or anywhere else.

Customize the loop with pre_get_posts

This hook enables developers to customize the $query object and customize the default WordPress Loop however is required. Here is a quick example of how it works:

Above mentioned function filters the loop via pre_get_posts loop. we use conditional tags and methods to check if the request is on the front-end for the main loop (via is_main_query()). If that condition is met, we set the number of posts_per_page to 1.  There are many various methods available

When to use?

The pre_get_posts filter hook is useful when you want to customize the main WP Loop.

30-Second Summary

  • To customize the default loop, use pre_get_posts
  • To modify loops and/or create multiple loops, use WP_Query()
  • To modify the default loop, use query_posts()
  • To create static, additional loops, use get_posts()
If you’re working with WordPress loops and want to learn more about using them to customize your theme, we cover the topic extensively in our book, Theme hand book.