Title: Weird comments behavior after using WP_query()
Last modified: August 19, 2016

---

# Weird comments behavior after using WP_query()

 *  Resolved [gianko_olivieri](https://wordpress.org/support/users/gianko_olivieri/)
 * (@gianko_olivieri)
 * [17 years, 10 months ago](https://wordpress.org/support/topic/weird-comments-behavior-after-using-wp_query/)
 * so.. i modified my single.php file to show the latest five post in that category
   this way:
 *     ```
       <?php  $my_query = new WP_Query('cat=' . $numero. '&showposts=5');
         while ($my_query->have_posts()) : $my_query->the_post();
         $do_not_duplicate = $post->ID;?>
           <a href="<?php the_permalink() ?>" rel="bookmark" title="Link Permanente a <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
   
         <?php endwhile; ?>
       ```
   
 * that code is after the loop and before the comments.
 * And now the comments in one post appear in other posts.. haha…
 * the problem, i guest, is in this line:
    $my_query = new WP_Query(‘cat=’ . $numero.‘&
   showposts=5’);
 * Help, please 🙂

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Roger Theriault](https://wordpress.org/support/users/rogertheriault/)
 * (@rogertheriault)
 * [17 years, 10 months ago](https://wordpress.org/support/topic/weird-comments-behavior-after-using-wp_query/#post-814548)
 * When you’re done your “latest five posts” loop, try
 *     ```
       <?php if (have_posts()) : while (have_posts()) : the_post();
             update_post_caches($posts); ?>
          <!-- Do comment stuff here... -->
         <?php endwhile; endif; ?>
       ```
   
 * This is all documented on [http://codex.wordpress.org/The_Loop#Multiple_Loops](http://codex.wordpress.org/The_Loop#Multiple_Loops)
   but you need to carefully read through all the examples to understand it, unfortunately.
 *  Thread Starter [gianko_olivieri](https://wordpress.org/support/users/gianko_olivieri/)
 * (@gianko_olivieri)
 * [17 years, 10 months ago](https://wordpress.org/support/topic/weird-comments-behavior-after-using-wp_query/#post-814556)
 * Yeah!!! thanks, that fixed it!…
 * here is the final code, if someone is having the same problem.
 *     ```
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
       	<!-- POST STUFF -->
       	<?php endwhile; else: ?>
       	<!-- 404 STUFF -->
       <?php endif; ?>
       <?php  $my_query = new WP_Query('cat=X&showposts=5');
       	while ($my_query->have_posts()) : $my_query->the_post();
       	$do_not_duplicate = $post->ID;?>
       	<!-- 5 LATEST POST STUFF -->
       	<?php endwhile; ?>
       <?php if (have_posts()) : while (have_posts()) : the_post();
       	update_post_caches($posts); ?>
       	<!-- COMMENTS STUFF -->
       <?php endwhile; endif; ?>
       ```
   
 * where X is the Cat ID
 *  [Marcomail](https://wordpress.org/support/users/marcomail/)
 * (@marcomail)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/weird-comments-behavior-after-using-wp_query/#post-814922)
 * I don’t have
 * `<?php if (have_posts()) : while (have_posts()) : the_post();`
 *  on comments.php
 * i only have
 * `<?php comments_template(); ?>`
 *  [Roger Theriault](https://wordpress.org/support/users/rogertheriault/)
 * (@rogertheriault)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/weird-comments-behavior-after-using-wp_query/#post-814926)
 * [@marcomail](https://wordpress.org/support/users/marcomail/), we’re talking about
   a posts query. comments.php is only supposed to display comments, not posts. 
   Look in single.php. That page typically includes comments.php, so on a single
   post page, it uses the $wp_query->comments list to display the comments for the
   related post.
 * If you’re writing a custom query (like the posters above), then we’re explaining
   that you need to restore the default $wp_query global variable, otherwise your
   comments_template() function call will use the most recent query data, and display
   the wrong comments or none at all.
 * I hope that’s why you asked your question.
 * BTW there is a have_comments() loop, but for comments.php customization you should
   stick with the default comments.php and just use CSS, or make a custom Walker
   class if you like to dig into PHP.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Weird comments behavior after using WP_query()’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [Comments](https://wordpress.org/support/topic-tag/comments/)
 * [wp-query](https://wordpress.org/support/topic-tag/wp-query/)

 * 4 replies
 * 3 participants
 * Last reply from: [Roger Theriault](https://wordpress.org/support/users/rogertheriault/)
 * Last activity: [17 years, 3 months ago](https://wordpress.org/support/topic/weird-comments-behavior-after-using-wp_query/#post-814926)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
