Title: Run WordPress loop within a loop
Last modified: August 21, 2016

---

# Run WordPress loop within a loop

 *  Resolved [Archie Makuwa](https://wordpress.org/support/users/archie22is/)
 * (@archie22is)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/run-wordpress-loop-within-a-loop/)
 * Hey guys,
 * I have a two custom post types:
    – Products – and Symptoms
 * The products are displayed in a excerpt format on the products page and grouped
   according the products category (custom taxonomy). The code is pasted on Pastebin
   on the following url: [http://pastebin.com/K1TvtgBA](http://pastebin.com/K1TvtgBA)
 * The product single page is as follows: [http://pastebin.com/ZJeRauJT](http://pastebin.com/ZJeRauJT)
 * The Question:
 * I want to run a custom query (I have tried: WP_Query & query() ) to return the
   custom posts within the associated symptoms div on the single page. It works,
   but breaks my single page loop. The moment I run the loop, I am unable to display:
   –
   Indications – and Contra-Indications
 * What is the best method to run the loop within the already existing loop. What
   the symptoms actually do is retrieve a bunch of related symptoms (related to 
   product based on custom taxonomy).
 * Someone said that I should not use (the_post) as that is the default WordPress
   post – tried that and I obtained the same results.
 * Any additional suggestions?

Viewing 1 replies (of 1 total)

 *  Thread Starter [Archie Makuwa](https://wordpress.org/support/users/archie22is/)
 * (@archie22is)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/run-wordpress-loop-within-a-loop/#post-4436904)
 * There was no need to run a loop within a loop on the single page. I used the 
   formula below:
 *     ```
       <?php
       	$terms = get_the_terms( $post->ID, 'symptoms_cat' );
       	foreach ( $terms as $term ) {
       		$x = $term->name;
       	}
       ?>
   
       	<!-- Execute loop -->
       	<?php
       		query_posts(
       			array(
       				'post_type' => 'symptom',
       				'category_name' => $x,
       				//'category_name' => '',
       				'posts_per_page' => 99
       			)
       		);
       	?>
   
       	<ul class="symptoms-list">
       		<?php while (have_posts()) : the_post(); ?>
       			<?php
       	        	$symptom_img = get_post_meta($post->ID, 'symptom_image', true);
       	          	$my_images = $symptom_img['guid'];
       	    	?>
       			<li>
       				<a href="<?php the_permalink(); ?>">
       	           		<img src="<?php echo $my_images; ?>" /> <br />
       					<?php the_title(); ?>
       				</a>
       			</li>
       		<?php endwhile; ?>
       	</ul>
       ```
   
 * Any questions or queries, holla back!!

Viewing 1 replies (of 1 total)

The topic ‘Run WordPress loop within a loop’ is closed to new replies.

## Tags

 * [custom loop](https://wordpress.org/support/topic-tag/custom-loop/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [multiple loops](https://wordpress.org/support/topic-tag/multiple-loops/)
 * [Post Types](https://wordpress.org/support/topic-tag/post-types/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Archie Makuwa](https://wordpress.org/support/users/archie22is/)
 * Last activity: [12 years, 4 months ago](https://wordpress.org/support/topic/run-wordpress-loop-within-a-loop/#post-4436904)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
