Bits Kingdom Logo

3 Ways to create a loop with WordPress

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

Are you tired of searching through countless forums for a straightforward way to loop through posts or products on your site? Whether you’re setting up a new blog or managing an e-commerce store, you’ll find that handling data efficiently is essential to keeping your site speedy and your users happy. Today, I’ll break down three powerful methods to create loops in WordPress: using the WP_Query class, the get_posts function, and the query_posts function. Let’s dive right in and untangle these options!

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/

Wrap-Up:

There you have it—three robust methods to loop through your WordPress content. Whether you’re a seasoned developer or just starting out, understanding these techniques can significantly streamline your site’s functionality. Remember, choosing the right method depends on your specific needs and how much control you want over your queries. Happy coding, and here’s to making your WordPress experience as smooth as possible!

Could it be interesting for you:

Web Apps with Rust: Implementing Secure Authentication and Authorization

Chapter 4: Enhancing security in your Rust web apps