Why favour the standard WP loop over iterating over (new WP_Query())->get_posts()?When should you use WP_Query vs query_posts() vs get_posts()?When to use WP_query(), query_posts() and pre_get_postsAdd class to every other posts using get_postsWP_Query() and get_posts() can't handle over a thousand posts?Trying to check and see if a post has a featured image outside of the main loopShow all posts using the template page and the loop?What's the difference between “get_posts” and “wp_get_recent_posts” when used with “setup_postdata”?Transient Loop Not working as expectedIs get_posts() more efficient than The Loop?Why WP_Query in functions.php is not working when get_posts works?get_posts works but new wp_query doesn'tHow To Use get_posts & get_the_post_thumbnail Outside The Loop

Can I have a delimited macro with a literal # in the parameter text?

Could a chemically propelled craft travel directly between Earth and Mars spaceports?

What's is the easiest way to purchase a stock and hold it

How could the B-29 bomber back up under its own power?

"File type Zip archive (application/zip) is not supported" when opening a .pdf file

Very serious stuff - Salesforce bug enabled "Modify All"

Is there any official Lore on Keraptis the Wizard, apart from what is in White Plume Mountain?

Germany rejected my entry to Schengen countries

Can the bitcoin lightning network support more than 8 decimal places?

Is it a good idea to teach algorithm courses using pseudocode instead of a real programming language?

Good examples of "two is easy, three is hard" in computational sciences

Difference between good and not so good university?

How to choose the correct exposure for flower photography?

Should I twist DC power and ground wires from a power supply?

pwaS eht tirsf dna tasl setterl fo hace dorw

How to fix "webpack Dev Server Invalid Options" in Vuejs

Failing students when it might cause them economic ruin

Hotel booking: Why is Agoda much cheaper than booking.com?

How do I unravel apparent recursion in an edef statement?

Precedent for disabled Kings

Bash Array of Word-Splitting Headaches

Why are Marine Le Pen's possible connections with Steve Bannon something worth investigating?

Is a reptile with diamond scales possible?

Managing heat dissipation in a magic wand



Why favour the standard WP loop over iterating over (new WP_Query())->get_posts()?


When should you use WP_Query vs query_posts() vs get_posts()?When to use WP_query(), query_posts() and pre_get_postsAdd class to every other posts using get_postsWP_Query() and get_posts() can't handle over a thousand posts?Trying to check and see if a post has a featured image outside of the main loopShow all posts using the template page and the loop?What's the difference between “get_posts” and “wp_get_recent_posts” when used with “setup_postdata”?Transient Loop Not working as expectedIs get_posts() more efficient than The Loop?Why WP_Query in functions.php is not working when get_posts works?get_posts works but new wp_query doesn'tHow To Use get_posts & get_the_post_thumbnail Outside The Loop






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















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?










share|improve this question






















  • 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
    3 hours ago

















2















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?










share|improve this question






















  • 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
    3 hours ago













2












2








2








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?










share|improve this question














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?







get-posts






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 3 hours ago









user3574603user3574603

222127




222127












  • 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
    3 hours ago

















  • 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
    3 hours ago
















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
3 hours ago





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
3 hours ago










2 Answers
2






active

oldest

votes


















1














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






share|improve this answer






























    1














    I advise you to take a look at the documentation:



    • The Loop | Theme Developer Handbook | WordPress Developer Resources

    you'll find more details there. But it follows a short overview:



    The Loop gives you access to:



    Template Tags




    • Template Tags | Theme Developer Handbook | WordPress Developer Resources

    • List of Template Tags | Theme Developer Handbook | WordPress Developer Resources

    Conditional Tags



    • Conditional Tags | Theme Developer Handbook | WordPress Developer Resources

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



    • What the Loop Can Display | The Loop | Theme Developer Handbook | WordPress Developer Resources

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



    • loop_start

    • the_post

    • loop_end

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




    • pre_get_posts

    And all the filters listed under:



    • WP_Query Filters | Plugin API/Filter Refernce | WordPress Codex

    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:



    • Multiple Loops | The Loop | Theme Developer Handbook | WordPress Developer Resources

    Because:



    • You can use rewind_posts() to loop through the same query a second time;


    • Or create a secondary query and loop, using wp_reset_postdata() in the process.


    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.



    Like I said in the beginning, this is more or less just an abstract of the documentation, with some additional information and links. So you know now where to go, if you want to read up on it. Of course for some things it will be best if you deep dive into the source code itself, depending on how far you want to go. Anyhow, this should get you going, and give you enough keywords on hand to go further.






    share|improve this answer























      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "110"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f338159%2fwhy-favour-the-standard-wp-loop-over-iterating-over-new-wp-query-get-posts%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      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






      share|improve this answer



























        1














        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






        share|improve this answer

























          1












          1








          1







          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






          share|improve this answer













          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 hours ago









          Tom J NowellTom J Nowell

          33.7k448100




          33.7k448100























              1














              I advise you to take a look at the documentation:



              • The Loop | Theme Developer Handbook | WordPress Developer Resources

              you'll find more details there. But it follows a short overview:



              The Loop gives you access to:



              Template Tags




              • Template Tags | Theme Developer Handbook | WordPress Developer Resources

              • List of Template Tags | Theme Developer Handbook | WordPress Developer Resources

              Conditional Tags



              • Conditional Tags | Theme Developer Handbook | WordPress Developer Resources

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



              • What the Loop Can Display | The Loop | Theme Developer Handbook | WordPress Developer Resources

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



              • loop_start

              • the_post

              • loop_end

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




              • pre_get_posts

              And all the filters listed under:



              • WP_Query Filters | Plugin API/Filter Refernce | WordPress Codex

              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:



              • Multiple Loops | The Loop | Theme Developer Handbook | WordPress Developer Resources

              Because:



              • You can use rewind_posts() to loop through the same query a second time;


              • Or create a secondary query and loop, using wp_reset_postdata() in the process.


              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.



              Like I said in the beginning, this is more or less just an abstract of the documentation, with some additional information and links. So you know now where to go, if you want to read up on it. Of course for some things it will be best if you deep dive into the source code itself, depending on how far you want to go. Anyhow, this should get you going, and give you enough keywords on hand to go further.






              share|improve this answer



























                1














                I advise you to take a look at the documentation:



                • The Loop | Theme Developer Handbook | WordPress Developer Resources

                you'll find more details there. But it follows a short overview:



                The Loop gives you access to:



                Template Tags




                • Template Tags | Theme Developer Handbook | WordPress Developer Resources

                • List of Template Tags | Theme Developer Handbook | WordPress Developer Resources

                Conditional Tags



                • Conditional Tags | Theme Developer Handbook | WordPress Developer Resources

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



                • What the Loop Can Display | The Loop | Theme Developer Handbook | WordPress Developer Resources

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



                • loop_start

                • the_post

                • loop_end

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




                • pre_get_posts

                And all the filters listed under:



                • WP_Query Filters | Plugin API/Filter Refernce | WordPress Codex

                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:



                • Multiple Loops | The Loop | Theme Developer Handbook | WordPress Developer Resources

                Because:



                • You can use rewind_posts() to loop through the same query a second time;


                • Or create a secondary query and loop, using wp_reset_postdata() in the process.


                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.



                Like I said in the beginning, this is more or less just an abstract of the documentation, with some additional information and links. So you know now where to go, if you want to read up on it. Of course for some things it will be best if you deep dive into the source code itself, depending on how far you want to go. Anyhow, this should get you going, and give you enough keywords on hand to go further.






                share|improve this answer

























                  1












                  1








                  1







                  I advise you to take a look at the documentation:



                  • The Loop | Theme Developer Handbook | WordPress Developer Resources

                  you'll find more details there. But it follows a short overview:



                  The Loop gives you access to:



                  Template Tags




                  • Template Tags | Theme Developer Handbook | WordPress Developer Resources

                  • List of Template Tags | Theme Developer Handbook | WordPress Developer Resources

                  Conditional Tags



                  • Conditional Tags | Theme Developer Handbook | WordPress Developer Resources

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



                  • What the Loop Can Display | The Loop | Theme Developer Handbook | WordPress Developer Resources

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



                  • loop_start

                  • the_post

                  • loop_end

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




                  • pre_get_posts

                  And all the filters listed under:



                  • WP_Query Filters | Plugin API/Filter Refernce | WordPress Codex

                  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:



                  • Multiple Loops | The Loop | Theme Developer Handbook | WordPress Developer Resources

                  Because:



                  • You can use rewind_posts() to loop through the same query a second time;


                  • Or create a secondary query and loop, using wp_reset_postdata() in the process.


                  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.



                  Like I said in the beginning, this is more or less just an abstract of the documentation, with some additional information and links. So you know now where to go, if you want to read up on it. Of course for some things it will be best if you deep dive into the source code itself, depending on how far you want to go. Anyhow, this should get you going, and give you enough keywords on hand to go further.






                  share|improve this answer













                  I advise you to take a look at the documentation:



                  • The Loop | Theme Developer Handbook | WordPress Developer Resources

                  you'll find more details there. But it follows a short overview:



                  The Loop gives you access to:



                  Template Tags




                  • Template Tags | Theme Developer Handbook | WordPress Developer Resources

                  • List of Template Tags | Theme Developer Handbook | WordPress Developer Resources

                  Conditional Tags



                  • Conditional Tags | Theme Developer Handbook | WordPress Developer Resources

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



                  • What the Loop Can Display | The Loop | Theme Developer Handbook | WordPress Developer Resources

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



                  • loop_start

                  • the_post

                  • loop_end

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




                  • pre_get_posts

                  And all the filters listed under:



                  • WP_Query Filters | Plugin API/Filter Refernce | WordPress Codex

                  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:



                  • Multiple Loops | The Loop | Theme Developer Handbook | WordPress Developer Resources

                  Because:



                  • You can use rewind_posts() to loop through the same query a second time;


                  • Or create a secondary query and loop, using wp_reset_postdata() in the process.


                  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.



                  Like I said in the beginning, this is more or less just an abstract of the documentation, with some additional information and links. So you know now where to go, if you want to read up on it. Of course for some things it will be best if you deep dive into the source code itself, depending on how far you want to go. Anyhow, this should get you going, and give you enough keywords on hand to go further.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  NicolaiNicolai

                  15.5k73887




                  15.5k73887



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to WordPress Development Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f338159%2fwhy-favour-the-standard-wp-loop-over-iterating-over-new-wp-query-get-posts%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                      Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                      Tom Holland Mục lục Đầu đời và giáo dục | Sự nghiệp | Cuộc sống cá nhân | Phim tham gia | Giải thưởng và đề cử | Chú thích | Liên kết ngoài | Trình đơn chuyển hướngProfile“Person Details for Thomas Stanley Holland, "England and Wales Birth Registration Index, 1837-2008" — FamilySearch.org”"Meet Tom Holland... the 16-year-old star of The Impossible""Schoolboy actor Tom Holland finds himself in Oscar contention for role in tsunami drama"“Naomi Watts on the Prince William and Harry's reaction to her film about the late Princess Diana”lưu trữ"Holland and Pflueger Are West End's Two New 'Billy Elliots'""I'm so envious of my son, the movie star! British writer Dominic Holland's spent 20 years trying to crack Hollywood - but he's been beaten to it by a very unlikely rival"“Richard and Margaret Povey of Jersey, Channel Islands, UK: Information about Thomas Stanley Holland”"Tom Holland to play Billy Elliot""New Billy Elliot leaving the garage"Billy Elliot the Musical - Tom Holland - Billy"A Tale of four Billys: Tom Holland""The Feel Good Factor""Thames Christian College schoolboys join Myleene Klass for The Feelgood Factor""Government launches £600,000 arts bursaries pilot""BILLY's Chapman, Holland, Gardner & Jackson-Keen Visit Prime Minister""Elton John 'blown away' by Billy Elliot fifth birthday" (video with John's interview and fragments of Holland's performance)"First News interviews Arrietty's Tom Holland"“33rd Critics' Circle Film Awards winners”“National Board of Review Current Awards”Bản gốc"Ron Howard Whaling Tale 'In The Heart Of The Sea' Casts Tom Holland"“'Spider-Man' Finds Tom Holland to Star as New Web-Slinger”lưu trữ“Captain America: Civil War (2016)”“Film Review: ‘Captain America: Civil War’”lưu trữ“‘Captain America: Civil War’ review: Choose your own avenger”lưu trữ“The Lost City of Z reviews”“Sony Pictures and Marvel Studios Find Their 'Spider-Man' Star and Director”“‘Mary Magdalene’, ‘Current War’ & ‘Wind River’ Get 2017 Release Dates From Weinstein”“Lionsgate Unleashing Daisy Ridley & Tom Holland Starrer ‘Chaos Walking’ In Cannes”“PTA's 'Master' Leads Chicago Film Critics Nominations, UPDATED: Houston and Indiana Critics Nominations”“Nominaciones Goya 2013 Telecinco Cinema – ENG”“Jameson Empire Film Awards: Martin Freeman wins best actor for performance in The Hobbit”“34th Annual Young Artist Awards”Bản gốc“Teen Choice Awards 2016—Captain America: Civil War Leads Second Wave of Nominations”“BAFTA Film Award Nominations: ‘La La Land’ Leads Race”“Saturn Awards Nominations 2017: 'Rogue One,' 'Walking Dead' Lead”Tom HollandTom HollandTom HollandTom Hollandmedia.gettyimages.comWorldCat Identities300279794no20130442900000 0004 0355 42791085670554170004732cb16706349t(data)XX5557367