I've a page that uses a loop over posts. Within the loop, I use $query->the_post(); and it works fine.

However, when I'm finished looping, the_post() is still set to the last post in my loop. So when I use have_comments(), I get the comments for that post instead of the comments for the current page -which is what I need. Does that make any sense?

What's the right way to solve this? have_comments() does not take any argument such as the ID of the current page.

  • please post the full code of the template; it might not contain any wp_reset_postdata() or so at the end ... – Michael Apr 19 '18 at 22:45

One way would be to rewind, with the rewind_posts() method, to reset $current_post and $post [docs]. There's also rewind_comments() to reset $current_comment and $comment (the current comment ID property) [src].

  • Thanks, I'll give that a shot. Does the rewind_posts() method basically come down to querying the current page twice (before and after my loop)? – RubenGeert Apr 19 '18 at 10:04
  • Calling the rewind_posts() method will not run another db query. (similar to rewind for iterators, but WP_Query doesn't implement such an interface, ... yet ;-) – birgire Apr 19 '18 at 10:38

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.