Title: WP_query loop after single post, before comments
Last modified: August 19, 2016

---

# WP_query loop after single post, before comments

 *  [nielskreijveld](https://wordpress.org/support/users/nielskreijveld/)
 * (@nielskreijveld)
 * [17 years ago](https://wordpress.org/support/topic/wp_query-loop-after-single-post-before-comments/)
 * Hello people.
 * I’m working on a theme that has a normal loop on the single.php page. After this
   loop, there is a custom WP_query loop that displays some posts from another category.
   Then after these posts the comments template should show up. My code is as follows:
 *     ```
       <?php
       if(have_posts()) :
       while(have_posts()) : the_post(); ?>
       // Show single post stuff
       <?php
       $my_query1 = new WP_Query('cat=6&showposts=4&orderby=rand');
       while ($my_query1->have_posts()) : $my_query1->the_post(); ?>
       // Show 4 posts from cat 6
       <?php endwhile; ?>
       <?php comments_template(); ?>
       <?php
       endwhile;
       endif;
       ?>
       ```
   
 * The comments template shows up, but it is “linked” to the last post of the custom
   query (the last post from category 6). So when I post a comment, it actually 
   gets posted to a different post (in category 6).
    I’ve tried adding <?php $wp_query
   = $temp_query; ?> before the custom loopt and <?php $temp_query = $wp_query; ?
   > after it, but that didn’t help.
 * How do I fix this?

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

 *  Thread Starter [nielskreijveld](https://wordpress.org/support/users/nielskreijveld/)
 * (@nielskreijveld)
 * [16 years, 12 months ago](https://wordpress.org/support/topic/wp_query-loop-after-single-post-before-comments/#post-1091110)
 * Bump, does anyone have any idea?
 *  [sebastian_a](https://wordpress.org/support/users/sebastian_a/)
 * (@sebastian_a)
 * [16 years, 12 months ago](https://wordpress.org/support/topic/wp_query-loop-after-single-post-before-comments/#post-1091121)
 * I’d like to know this one as well. I have been struggling with this a little.
   I have a category of post that I would like to be added to a page after the content
   of that page. However I cannot get this query to run nor display.
 *  [kristarella](https://wordpress.org/support/users/kristarella/)
 * (@kristarella)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/wp_query-loop-after-single-post-before-comments/#post-1091179)
 * Even though you `endwhile;` on the custom loop it is still active (as you can
   see by the comment behaviour). You need to re-enter the normal loop.
    The only
   way I’ve been able to do it is by sticking `while(have_posts()) { the_post();}`
   after the `endwhile;` for the custom loop.
 * Perhaps if that goes askew you should even end the main loop before the custom
   loop and then start it again.
 *  [sebastian_a](https://wordpress.org/support/users/sebastian_a/)
 * (@sebastian_a)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/wp_query-loop-after-single-post-before-comments/#post-1091219)
 * I found a solution and posted it here. Maybe it helps you as well:
    [http://wordpress.org/support/topic/276637?replies=2](http://wordpress.org/support/topic/276637?replies=2)

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

The topic ‘WP_query loop after single post, before comments’ is closed to new replies.

## Tags

 * [Comments](https://wordpress.org/support/topic-tag/comments/)
 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [custom loop](https://wordpress.org/support/topic-tag/custom-loop/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 3 participants
 * Last reply from: [sebastian_a](https://wordpress.org/support/users/sebastian_a/)
 * Last activity: [16 years, 8 months ago](https://wordpress.org/support/topic/wp_query-loop-after-single-post-before-comments/#post-1091219)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
