• Resolved yrsengar

    (@yrsengar)


    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)
  • Have you reviewed multiple Loops?

    Thread Starter yrsengar

    (@yrsengar)

    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.