3

The WP loop goes like this:

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

Why is it preferred over the following?

foreach( (new WP_Query())->get_posts() as $post ) { ... }

To me, it's more apparent what is going on in the latter. I'm new to PHP and WP and I'm trying to understand why I should be using the former.

What do I gain by using the standard loop? Is iterating over get_posts() any less efficient?

  • 3
    Because WordPress already sets the instance of WP_Query for you, calling new WP_Query() will create another instance, which will be an overhead. Read this and this. – Fayaz May 17 at 22:02
11

Several reasons

1. Filters and Actions

By using the standard loop, you execute various filters and actions that plugins rely on.

Additionally, you set up the_post correctly, allowing functions such as the_content etc to work correctly. Some filters can even insert "posts" into the loop

2. Memory Efficiency

By fetching all the posts as an array, you're forcing WP_Query to take the data it has and create WP_Post objects. With a standard post loop these are created as they're needed

3. PHP Warnings

Your loop doesn't check if any posts were actually found, so it's impossible to give a "No posts available" style message. It'll also generate PHP warnings at times.

4. Overriding WP globals

By using $post you're overriding a global variable, which can have unintended consequences, especially if this loop is nested inside another loop you're unaware of

5. PHP Efficiency and Correctness

Creating an object inside a foreach condition is bad practice, as is creating and then using an object without error checking.

6. Debugging

A lot of tools and plugins assume you're using a standard loop and have been built to make life easier. By doing this you're throwing all of that away

7. There are Better Alternatives

array_walk

A crude but superior option to your foreach might actually be array_walk:

$q = new WP_Query([ ..args ]);
array_walk( $q->get_posts(), function( $post ) {
    //
});

Note that I don't recommend using array_walk.

PHP Generators

Now that's not to say you couldn't use a different style loop while still having all the advantages.

For example WP Scholar has an article showing a php generator based loop:

if ( have_posts() ) {
   foreach ( wp_loop() as $post ) {
      echo '<h1>' . esc_html( get_the_title() ) . '</h1>';
   }
} else {
   echo '<h1>No posts found!</h1>';
}

This has the advantage that it still calls all the functions of a standard loop, but they've been abstracted away.

https://wpscholar.com/blog/creating-better-wordpress-loop/

I'm sure there are others, but a standard loop is reliable predictable and readable to all

4

I advise you to take a look at the documentation:

you'll find more details there.

It follows a short abstract:

The Loop gives you access to:

Template Tags

Conditional Tags

Not all Template and Conditional Tags are The Loop dependent, for an overview for what to use in The Loop:

Additionally, there are hooks that can be used with The Loop, like:

Looking at it a bit more broadly you could include something like:

And all the filters listed under:

Although the latter two technically do apply to a custom iteration over the $wp_query->get_posts() array too, but it is part of the system or process. And as a general rule it works better and more reliable if you use it inside the paradigm, so in this case by making use of The Loop.

Even if you need, sometimes it is unavoidable, additional loops, you don't have to do it outside The Loop paradigm:

Because:

Doing it this way gives you the benefits that come with The Loop, it is better integrated into how WordPress intended on doing it, and will create less overhead in the process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.