0

I have two loops in my code, why do I need to use wp_reset_postdata(); after each if it doesnt make any difference?

The definition: After looping through a separate query, this function restores the $post global to the current post in the main query.

So if I create a query that queries post_types, or categories or anything other than the post type="post_type" (which is the default parameter of the loop) then the wp_reset_postdata(); restores the post_type to "post_type"? If so how would that look in code, or is it nonsense that i just wrote?

Regardless can you give me an example of it?

        <div class="one-half">
                    <h2>Latest Opinion</h2>
                        <?php // opinion posts loop begins here
                        $opinionPosts = new WP_Query('cat=1&posts_per_page=2');

                        if ($opinionPosts->have_posts()) :
                            while ($opinionPosts->have_posts()) : $opinionPosts->the_post(); ?>
                                <div class="post-item clearfix">
                                        <?php the_excerpt(); ?>
                                </div>
                            <?php endwhile;
                            else : // fallback no content message here
                        endif;
                        wp_reset_postdata(); ?>
        </div><!-- /one-half -->

        <div class="one-half last">
                    <h2>Latest News</h2>
                        <?php // news posts loop begins here
                        $newsPosts = new WP_Query('cat=23&posts_per_page=2');

                        if ($newsPosts->have_posts()) :
                            while ($newsPosts->have_posts()) : $newsPosts->the_post(); ?>                           
                                <div class="post-item clearfix">
                                    <?php the_excerpt(); ?>
                                    </div><!-- /post-item -->
                            <?php endwhile;
                            else :  // fallback no content message here
                        endif;
                        wp_reset_postdata();
        </div> <!-- /one-half -->

                    ?>
1

You are missing the point of restoring the context to original state.

Yes, your loops do work without reset. But anything that tries to access queried post after them will hit leftovers of your custom queries in global variables, rather than post from main query.

In a literal sense the reset after your first query in this specific code is redundant, since there is nothing else done and you just proceed to do another query. However it is a good idea to keep it, because in case of future changes to the code the order of things might very well might change and this will suddenly cause an issue unnecessarily.

  • What would I need to add to it in order for it to be actually useful? can you give me a bit more specifc example? – john-thomas Jan 25 '17 at 21:20
  • It is actually useful — it prevents your queries from leaving overridden global state after them. Without it any template tag call after them would produce unexpected and invalid results. – Rarst Jan 25 '17 at 21:54

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.