Title: Nesting WP loop [2 loops inside one]
Last modified: August 21, 2016

---

# Nesting WP loop [2 loops inside one]

 *  Resolved [yrsengar](https://wordpress.org/support/users/yrsengar/)
 * (@yrsengar)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/nesting-wp-loop-2-loops-inside-one/)
 * Hi Guys,
    I am trying to nest two loops in one loop. The two loops below are 
   pulling content from custom posts and working perfectly individually. **1st loop:**———————————————-
 *     ```
       <?php query_posts( array( 'post_type' => 'stories') );
          while (have_posts()) : the_post();?><!-- begin of loop -->
   
             <section class="story-sec">
                <h3><?php the_title() ?> <span><?php the_field('title_2nd_row'); ?></span></h3>
                 <?php the_content() ?>
             </section>
   
       <?php endwhile; wp_reset_query(); ?><!-- end of loop -->
       ```
   
 * **
    2nd loop:
   
    ———————————————–
 *     ```
       <?php query_posts( array( 'post_type' => 'products') );
             while (have_posts()) : the_post();?><!-- begin of loop -->
   
               <div class="product" style="background: rgba(<?php the_field('box_color'); ?>)">
                   <h3><span><?php the_title(); ?></span><?php the_field('product_name_2nd_line'); ?></h3>
                   <p><?php the_field('product_description'); ?></p>
               </div>
   
       <?php endwhile; wp_reset_query(); ?><!-- end of loop -->
       ```
   
 * ————————————————-
    I want to add these two loop in one so the most latest post
   from any one of these custom post stays on TOP. Does someone here knows how to
   do this? Thank you!!

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/nesting-wp-loop-2-loops-inside-one/#post-4250517)
 * Have you reviewed [multiple Loops](http://codex.wordpress.org/The_Loop#Multiple_Loops)?
 *  Thread Starter [yrsengar](https://wordpress.org/support/users/yrsengar/)
 * (@yrsengar)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/nesting-wp-loop-2-loops-inside-one/#post-4250519)
 * By referring that multiple loop documentation this is what i did:
 *     ```
       <?php if (have_posts()) : ?>
   
       <?php query_posts( array( 'post_type' => 'stories') );
          while (have_posts()) : the_post();?><!-- begin of loop -->
             <section class="story-sec">
                <h3><?php the_title() ?> <span><?php the_field('title_2nd_row'); ?></span></h3>
                 <?php the_content() ?>
             </section>
       <?php endwhile; wp_reset_query(); ?><!-- end of loop -->
   
       <?php query_posts( array( 'post_type' => 'products') );
             while (have_posts()) : the_post();?><!-- begin of loop -->
               <div class="product" style="background: rgba(<?php the_field('box_color'); ?>)">
                   <h3><span><?php the_title(); ?></span><?php the_field('product_name_2nd_line'); ?></h3>
                   <p><?php the_field('product_description'); ?></p>
               </div>
       <?php endwhile; wp_reset_query(); ?><!-- end of loop -->
   
       <?php endif; ?>
       ```
   
 * But it has the same result as when i put them individually one above the other
   without nesting in another loop.
    If i add new entry in 2nd loop it should come
   on top (top of even first lopp entries) but first loop entry come on top nad 
   2nd loop below it doesn’t matter if the entry in 2nd loop is the latest one.

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

The topic ‘Nesting WP loop [2 loops inside one]’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [yrsengar](https://wordpress.org/support/users/yrsengar/)
 * Last activity: [12 years, 7 months ago](https://wordpress.org/support/topic/nesting-wp-loop-2-loops-inside-one/#post-4250519)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
