- Default Loop
- Loop with get_posts()
- Customize the Loop with pre_get_posts
- Loop with query_posts()
- Loop with WP_Query()
- 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_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?
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
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:
When to 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
query_posts, it’s possible to create loops that display just about anything.
When to use?
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?
pre_get_posts filter hook is useful when you want to customize the main WP Loop.
- To customize the default loop, use
- To modify loops and/or create multiple loops, use
- To modify the default loop, use
- To create static, additional loops, use