Title: Query Post Multiple Loops
Last modified: August 19, 2016

---

# Query Post Multiple Loops

 *  [sputnick3k](https://wordpress.org/support/users/sputnick3k/)
 * (@sputnick3k)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/query-multiple-loops/)
 * Ok so I know you can only query a loop once and then you need to create a new
   query for any additional loop or use get_post. I am building out a page that 
   requires 8 loops. And I really need to use the **if** attribute in case one of
   the loops doesn’t have any content which will be the case at times. From what
   I can tell, you can’t use the **if** attribute after the main post. This is what
   I have:
 *     ```
       <!-- SUNDAY LOOP -->
       <?php query_posts( array(
       	'post_type' => 'programming',
       	'schedule' => 'sunday',
       	'orderby' => menu_order )); ?>
       <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- MONDAY LOOP -->
       <?php query_posts( array(
       	'post_type' => 'programming',
       	'schedule' => 'monday',
       	'orderby' => menu_order ));
       if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- TUESDAY LOOP -->
       <?php query_posts( array(
       	'post_type' => 'programming',
       	'schedule' => 'tuesday',
       	'orderby' => menu_order ));
       if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- WEDNESDAY LOOP -->
       <?php query_posts( array(
       	'post_type' => 'programming',
       	'schedule' => 'wednesday',
       	'orderby' => menu_order ));
       if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- THURSDAY LOOP -->
       <?php query_posts( array(
       	'post_type' => 'programming',
       	'schedule' => 'thursday',
       	'orderby' => menu_order ));
       if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- FRIDAY LOOP -->
       <?php query_posts( array(
       	'post_type' => 'programming',
       	'schedule' => 'friday',
       	'orderby' => menu_order ));
       if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- SATURDAY LOOP -->
       <?php query_posts( array(
       	'post_type' => 'programming',
       	'schedule' => 'saturday',
       	'orderby' => menu_order ));
       if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
       ```
   
 * So far this works for me but I know I’m not marking this up correctly. Can someone
   advise?

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

 *  Thread Starter [sputnick3k](https://wordpress.org/support/users/sputnick3k/)
 * (@sputnick3k)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/query-multiple-loops/#post-1739888)
 * I ended up switching to this:
 *     ```
       <!-- SUNDAY LOOP -->
       <?php $my_query = new WP_Query( array(
       	'post_type' => 'programming',
       	'schedule' => 'sunday',
       	'orderby' => menu_order ));
       if (($my_query->post_count) > 0) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- MONDAY LOOP -->
       <?php $my_query = new WP_Query( array(
       	'post_type' => 'programming',
       	'schedule' => 'monday',
       	'orderby' => menu_order ));
       if (($my_query->post_count) > 0) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- TUESDAY LOOP -->
       <?php $my_query = new WP_Query( array(
       	'post_type' => 'programming',
       	'schedule' => 'tuesday',
       	'orderby' => menu_order ));
       if (($my_query->post_count) > 0) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- WEDNESDAY LOOP -->
       <?php $my_query = new WP_Query( array(
       	'post_type' => 'programming',
       	'schedule' => 'wednesday',
       	'orderby' => menu_order ));
       if (($my_query->post_count) > 0) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- THURSDAY LOOP -->
       <?php $my_query = new WP_Query( array(
       	'post_type' => 'programming',
       	'schedule' => 'thursday',
       	'orderby' => menu_order ));
       if (($my_query->post_count) > 0) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- FRIDAY LOOP -->
       <?php $my_query = new WP_Query( array(
       	'post_type' => 'programming',
       	'schedule' => 'friday',
       	'orderby' => menu_order ));
       if (($my_query->post_count) > 0) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- SATURDAY LOOP -->
       <?php $my_query = new WP_Query( array(
       	'post_type' => 'programming',
       	'schedule' => 'saturday',
       	'orderby' => menu_order ));
       if (($my_query->post_count) > 0) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
       ```
   
 * Both codes were working for me but this second one seems to be the one people
   suggest. Thoughts if you have them would be great! The documentation isn’t really
   clear an the proper method for using multiple loops. Especially if you need the
   conditional statements intact on each loop.
 *  Thread Starter [sputnick3k](https://wordpress.org/support/users/sputnick3k/)
 * (@sputnick3k)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/query-multiple-loops/#post-1739889)
 * So I ended up switching the code to:
 *     ```
       <!-- SUNDAY LOOP -->
       <?php $my_query = new WP_Query( array(
       	'post_type' => 'programming',
       	'schedule' => 'sunday',
       	'orderby' => menu_order ));
       if (($my_query->post_count) > 0) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- MONDAY LOOP -->
       <?php $my_query = new WP_Query( array(
       	'post_type' => 'programming',
       	'schedule' => 'monday',
       	'orderby' => menu_order ));
       if (($my_query->post_count) > 0) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- TUESDAY LOOP -->
       <?php $my_query = new WP_Query( array(
       	'post_type' => 'programming',
       	'schedule' => 'tuesday',
       	'orderby' => menu_order ));
       if (($my_query->post_count) > 0) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- WEDNESDAY LOOP -->
       <?php $my_query = new WP_Query( array(
       	'post_type' => 'programming',
       	'schedule' => 'wednesday',
       	'orderby' => menu_order ));
       if (($my_query->post_count) > 0) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- THURSDAY LOOP -->
       <?php $my_query = new WP_Query( array(
       	'post_type' => 'programming',
       	'schedule' => 'thursday',
       	'orderby' => menu_order ));
       if (($my_query->post_count) > 0) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- FRIDAY LOOP -->
       <?php $my_query = new WP_Query( array(
       	'post_type' => 'programming',
       	'schedule' => 'friday',
       	'orderby' => menu_order ));
       if (($my_query->post_count) > 0) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
   
       <!-- SATURDAY LOOP -->
       <?php $my_query = new WP_Query( array(
       	'post_type' => 'programming',
       	'schedule' => 'saturday',
       	'orderby' => menu_order ));
       if (($my_query->post_count) > 0) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt('...read more'); ?>
   
       <p><?php $key="program_time"; echo get_post_meta($post->ID, $key, true); ?> EST</p>
   
       <?php endwhile; ?>
       <?php else : ?>
       <p>Healthy programming coming soon!</p>
       <?php endif; ?>
       ```
   
 * This seems to be the proper markup but I’m not sure. The documentation on using
   multiple loops is unclear. There are several ways to use multiple loops in the
   WP Codex but some of them seem to contradict each other. It is especially unclear
   if you want to keep the conditional statements intact. Could someone advise?

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

The topic ‘Query Post Multiple Loops’ is closed to new replies.

## Tags

 * [multiple loops](https://wordpress.org/support/topic-tag/multiple-loops/)
 * [post](https://wordpress.org/support/topic-tag/post/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 1 participant
 * Last reply from: [sputnick3k](https://wordpress.org/support/users/sputnick3k/)
 * Last activity: [15 years, 7 months ago](https://wordpress.org/support/topic/query-multiple-loops/#post-1739889)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
