3 Ways to create a loop with WordPress

Here’s how to create your queries in a fast and easy way.

March 14, 2022

By: Michael Cardoza

If you are looking for a solution to query a list of articles in a blog or a list of e-commerce products, here are 3 options: with WP_Query class, get_posts, or query_posts functions. 

WP_Query (Class):

WP_Query is a class used to perform database queries while maintaining the query secure and simple. In addition, it allows us to pass an array with parameters as a single argument when creating the instance of WP_Query.

WP_Query not only returns an array with the available posts, but it also provides us with properties and methods to get more information about our query, such as: 

  • $found_posts : shows the total number of posts found that match the parameters of the query.
  • $posts : displays the posts requested from the database.
  • have_posts() : determines if there are more posts available in the loop.
  • have_comments() : shows if there are comments available.

On one hand, this class gives us wide flexibility for our database queries. On the other hand, it is also used by the functions get_posts() and query_posts().

Let’s first take a look at what a basic loop looks like in WordPress:

if( have_posts() ) {
    while( have_posts() ) {
        the_post();

        the_title();
        the_content();
    }
}

Now let’s see how a loop with WP_Query and customizable looks like:

$args = [
    'post_type' => 'post',
    'posts_per_page' => 15
];
$query = new WP_Query($args);

if( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $post->the_post();

        the_title();
        the_content();
    }
}

Here you can learn about the correct use of WP_Query – Docs: https://developer.wordpress.org/reference/classes/wp_query/

But if you need something simpler, you will probably be interested in using the get_posts() function that we will discuss next.

Get Posts (Function):

get_posts() is a function that returns an array of objects of the type WP_Post that comes with the data from our query. 

As we mentioned, this function uses the WP_Query class in the background, with some optimizations and variations. It helps us to simplify some code lines and, like the WP_Query class, it also accepts an array of parameters as an argument.

$args = [
    'post_type' => 'post',
    'posts_per_page' => 15
];
$items = get_posts($args);

foreach ($items as $item) {
    echo '<h2>' . get_the_title($item->ID) .  '</h2>';
    echo '<div>' . get_the_content($item->ID) .  '</div>';
}

Here you can learn more about the correct usage of get_posts() – Docs: https://developer.wordpress.org/reference/functions/get_posts/

Query Posts:

query_posts() is a way to alter the main query used in WordPress, so we can pass new parameters for the query.

To clean up our query after a query_posts call, we must use wp_reset_query() and the main query resets. If we are not careful to end with wp_reset_query() we can create an infinite loop, with all the terrible consequences you can imagine: creating a problem on the server or the hosting, and its subsequent overload. 

Warning: when developing a theme or plugin, this function should be used as little as possible.

$args = [
    'post_type' => 'post',
    'posts_per_page' => 15
];

query_posts($args);

if( have_posts() ) {
    while( have_posts() ) {
        the_post();

        the_title();
        the_content();
    }
}

wp_reset_query();

Here you can learn more about the correct use of query_posts() – Docs: https://developer.wordpress.org/reference/functions/query_posts/