Title: Multiple loop: problem with $post
Last modified: August 19, 2016

---

# Multiple loop: problem with $post

 *  [DennisMadsen](https://wordpress.org/support/users/dennismadsen/)
 * (@dennismadsen)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/multiple-loop-problem-with-post/)
 * I have a problem with multiple loops inside my custom template:
 *     ```
       <?php
       get_header();
   
       $my_query = new WP_Query('showposts='.$slide_count.'&cat=5');
       while ($my_query->have_posts()) : $my_query->the_post();
       	//do something
       endwhile;
   
       $category_content = get_post_meta($post->ID, 'category_content', true);
       $my_query = new WP_Query('orderby=ID&order=ASC&cat='.$category_content);
       while ($my_query->have_posts()) : $my_query->the_post();
       	?>
       	<h2><?php the_title() ?></h2>
       	<?php the_content() ?>
       	<?php
       endwhile;		
   
       get_footer(); ?>
       ```
   
 * The above is a example of it. After the first while I try to access the property_
   $post->ID_ in the _get\_post\_meta_. I would like the ID of the page I’m showing–
   but I get the last ID from the above loop. How can I prevent the loops from overwriting
   the loop that WordPress generates?
 * Please tell me, if you do not understand my problem. I think is a bit complicated
   to explain 🙂
    Thanks!

Viewing 1 replies (of 1 total)

 *  Thread Starter [DennisMadsen](https://wordpress.org/support/users/dennismadsen/)
 * (@dennismadsen)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/multiple-loop-problem-with-post/#post-1643394)
 * I found a hack, but I don’t know if it’s a good solution:
 *     ```
       <?php get_header();
       echo "Post id: ".$post->ID."<br/><br/>";
   
       $category = get_the_category();
       $my_query = new WP_Query('cat='.$category[0]->cat_name);
       while ($my_query->have_posts()) : $my_query->the_post();
       	echo get_the_title()."<br>";
       endwhile;
   
       $post = $wp_query->post;
   
       echo "<br/>Post id: ".$post->ID;
   
       get_footer(); ?>
       ```
   
 * Because of the _$post = $wp\_query->post;_, I can go back to the original $post,
   after the multiple loops has ended. Are there other solutions? The post id printed
   in the top and bottom should be the same.

Viewing 1 replies (of 1 total)

The topic ‘Multiple loop: problem with $post’ is closed to new replies.

 * 1 reply
 * 1 participant
 * Last reply from: [DennisMadsen](https://wordpress.org/support/users/dennismadsen/)
 * Last activity: [15 years, 9 months ago](https://wordpress.org/support/topic/multiple-loop-problem-with-post/#post-1643394)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
