Title: Pulling Recent Posts in Category inside loop causing issues
Last modified: August 19, 2016

---

# Pulling Recent Posts in Category inside loop causing issues

 *  Resolved [pshero](https://wordpress.org/support/users/pshero/)
 * (@pshero)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/pulling-recent-posts-in-category-inside-loop-causing-issues/)
 * Hey Gang,
    I’m using the following code to pull a list of recent posts in the
   current category. The problem is that after this little block come the comments
   and for some reason it’s pulling the comments from the last of the recent posts
   I’m calling rather than the comments from the current post and I’m not enough
   of a PHP guy to understand why.
 * Any help would be GREATLY appreciated. THANKS! Here’s the code:
 *     ```
       <?php
       	// get other posts from this category only as related posts //
           $categories = get_the_category($post->ID);
           if ($categories) {
               $category_ids = array();
               foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
   
               $args = array (
                   'category__in' => $category_ids,
                   'post__not_in' => array($post->ID),
                   'showposts'=>6, // Number of related posts that will be shown.
                   'caller_get_posts'=>1
               );
   
               $my_query = new WP_query($args);
               if( $my_query->have_posts() ) {
                   while ($my_query->have_posts()) {
                       $my_query->the_post();
             ?>
                       <div>
                       <?php $image = get_post_meta($post->ID, 'article_image', true); ?>
                       <a href="<?php the_permalink($post->ID); ?>"><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>" style="height: 90px; width: 90px; float: left; margin: 8px 8px 0px 0px; border: 1px black solid; padding: 0px;"></a>
                       </div>
       			<?php
                   }
               }
           }
           ?>
       ```
   

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

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/pulling-recent-posts-in-category-inside-loop-causing-issues/#post-1356864)
 * This might help: [http://codex.wordpress.org/The_Loop#Multiple_Loops_Example_2](http://codex.wordpress.org/The_Loop#Multiple_Loops_Example_2),
   especially the part about useing ‘clone’.
 *  Thread Starter [pshero](https://wordpress.org/support/users/pshero/)
 * (@pshero)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/pulling-recent-posts-in-category-inside-loop-causing-issues/#post-1356913)
 * After a ton of looking I found the solution in 7sins reply to this post [HERE](http://wordpress.org/support/topic/271901?replies=11)
   where he saves the current post ID as a variable called $backup… Here’s my working
   code in case it helps anyone in the future.
 *     ```
       <?php
       	// get other posts from this category only as related posts //
       	$backup = $post;  // backup the current object
           $categories = get_the_category($post->ID);
           if ($categories) {
               $category_ids = array();
               foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
   
               $args = array (
                   'category__in' => $category_ids,
                   'post__not_in' => array($post->ID),
                   'showposts'=>6, // Number of related posts that will be shown.
                   'caller_get_posts'=>1
               );
   
               $my_query = new wp_query($args);
               if( $my_query->have_posts() ) {
                   while ($my_query->have_posts()) : $my_query->the_post();
             ?>
                       <div>
                       <?php $image = get_post_meta($post->ID, 'article_image', true); ?>
                       <a href="<?php the_permalink($post->ID); ?>"><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>" style="height: 90px; width: 90px; float: left; margin: 8px 8px 0px 0px; border: 1px black solid; padding: 0px;"></a>
                       </div>
       			<?php
       		endwhile;
       	  }
       	  $post = $backup;  // copy it back
       	  wp_reset_query(); // to use the original query again
       	}
       	?>
       ```
   

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

The topic ‘Pulling Recent Posts in Category inside loop causing issues’ is closed
to new replies.

 * 2 replies
 * 2 participants
 * Last reply from: [pshero](https://wordpress.org/support/users/pshero/)
 * Last activity: [16 years, 4 months ago](https://wordpress.org/support/topic/pulling-recent-posts-in-category-inside-loop-causing-issues/#post-1356913)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
