Title: Multiple Loops not working
Last modified: October 11, 2017

---

# Multiple Loops not working

 *  [manualmode](https://wordpress.org/support/users/manualmode/)
 * (@manualmode)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/multiple-loops-not-working/)
 * hello experts, need your helps 🙂
    my homepage needs two loops(first loop includes
   posts from featured category, second loop includes just normal posts) , so i 
   searched the codex, found the following code exactly match my needs.
 *     ```
       <?php $my_query = new WP_Query( 'category_name=featured&posts_per_page=1' );
       while ( $my_query->have_posts() ) : $my_query->the_post();
       $do_not_duplicate = $post->ID; ?>
       	<!-- Do stuff... -->
       <?php endwhile; ?>
       	<!-- Do other stuff... -->
       <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 
       if ( $post->ID == $do_not_duplicate ) continue; ?>
       	<!-- Do stuff... -->
       <?php endwhile; endif; ?>
       ```
   
 * However, after i modified my theme’s loop code, the result is that “the first
   loop and second display fine on homepage, but when i click the second page of
   posts, the same posts from first loop also display on top of that. as well as
   the third page…”
    the following is my theme’s original loop code
 *     ```
       if ( ! defined( 'WPINC' ) ) {
       	die;
       }
   
       get_header(); ?>
   
       <div id="content-blog" class="content-area">
       <main id="main" class="site-main" role="main" itemprop="mainContentOfPage" itemscope="itemscope" itemtype="http://schema.org/Blog">
   
       <?php do_action( 'responsive_mobile_blog_title' ); ?>
   
       <?php if ( have_posts() ) : ?>
   
       <?php get_template_part( 'template-parts/loop-header' ); ?>
   
       <?php /* Start the Loop */ ?>
       <?php while ( have_posts() ) : the_post(); ?>
   
       <?php
       /* Include the Post-Format-specific template for the content.
       * If you want to override this in a child theme, then include a file
       * called content-___.php (where ___ is the Post Format name) and that will be used instead.
        */
       get_template_part( 'template-parts/content', get_post_format() );
       						?>
   
       <?php endwhile; ?>
   
       <?php responsive_mobile_paging_nav(); ?>
   
       <?php else : ?>
   
       <?php get_template_part( 'template-parts/content', 'none' ); ?>
   
       <?php endif; ?>
   
       </main><!-- #main -->
       <?php get_sidebar(); ?>
       </div><!-- #content-blog -->
       <?php get_footer(); ?>
       ```
   
 * can anyone show me the correct code of displaying these 2 loops for my theme.
 * Thank you so much.
    -  This topic was modified 8 years, 8 months ago by [manualmode](https://wordpress.org/support/users/manualmode/).
    -  This topic was modified 8 years, 8 months ago by [manualmode](https://wordpress.org/support/users/manualmode/).
    -  This topic was modified 8 years, 8 months ago by [manualmode](https://wordpress.org/support/users/manualmode/).
    -  This topic was modified 8 years, 8 months ago by [manualmode](https://wordpress.org/support/users/manualmode/).
    -  This topic was modified 8 years, 8 months ago by [manualmode](https://wordpress.org/support/users/manualmode/).

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

 *  [Jacob Martella](https://wordpress.org/support/users/arenapigskin/)
 * (@arenapigskin)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/multiple-loops-not-working/#post-9578195)
 * Hey,
    So first I have a couple of questions so I can get a better sense for your
   problem and hopefully make it easier to find the solution. First, do you have
   a link to your site so I can what’s going on? Second, what do you want the first
   loop to do when the user goes to the second, third, etc., page?
 * At the moment, you definitely want to put [wp_reset_query();](https://developer.wordpress.org/reference/functions/wp_reset_query/)
   between the two loops. This helps keep them separate from one another.
 *  Thread Starter [manualmode](https://wordpress.org/support/users/manualmode/)
 * (@manualmode)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/multiple-loops-not-working/#post-9579259)
 * Hi ,
    Thanks for replying. Sorry, i am doing it on the localhost. I only want
   to display 1 post from “featured” category on the top of homepage (kind like 
   sticky post). But do not want it duplicates in the normal loop. However, now,
   it does’t duplicate in the normal loop. but it displayed on the top of every 
   post page e.g. 2nd and 3rd page of normal loop (kind like same sticky post now
   is displaying on each page of the loop). below is my modified code. i used the
   wp_reset_query(); as suggested but seem not working. Sorry, not an expert on 
   php and wordpress if the code looks silly…
 *     ```
       <?php do_action( 'responsive_mobile_blog_title' ); ?>
   
       <?php if ( have_posts() ) : ?>
   
       <?php get_template_part( 'template-parts/loop-header' ); ?>
   
       <?php /* Start the Loop */ ?>
       <?php $my_query = new WP_Query( 'category_name=featured&posts_per_page=1' );
       while ( $my_query->have_posts() ) : $my_query->the_post();
       $do_not_duplicate = $post->ID; ?>
   
       <?php get_template_part( 'template-parts/content', get_post_format() );?>
   
       <?php endwhile; ?>
   
       <?php wp_reset_query();?>
   
       <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 
       if ( $post->ID == $do_not_duplicate ) continue; ?>
   
       <?php get_template_part( 'template-parts/content', get_post_format() );?>
   
       <?php endwhile; endif; ?>
   
       <?php responsive_mobile_paging_nav(); ?>
   
       <?php else : ?>
   
       <?php get_template_part( 'template-parts/content', 'none' ); ?>
       <?php endif; ?>
       ```
   
 * Thank you so much.
 *  [Jacob Martella](https://wordpress.org/support/users/arenapigskin/)
 * (@arenapigskin)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/multiple-loops-not-working/#post-9580932)
 * No worries. It’s good you’re doing this on a local development. Makes it easier
   to fix mistakes and find what doesn’t work.
 * Anyway, I think this might do the trick for you. You’ll want to wrap the first
   loop inside an if statement with is_paged. So your code would look like this.
 *     ```
       if ( is_paged() == false ) {
         <?php /* Start the Loop */ ?>
         <?php $my_query = new WP_Query( 'category_name=featured&posts_per_page=1' );
         while ( $my_query->have_posts() ) : $my_query->the_post();
         $do_not_duplicate = $post->ID; ?>
   
         <?php get_template_part( 'template-parts/content', get_post_format() );?>
   
         <?php endwhile; ?>
       }
       ```
   
 * This should run a check to see if this is the second, third, etc., page and returns
   true if so and false if it’s the first page. So if it’s true it should hide the
   featured post section.
 * I hope this helps let me know how it works on your local site.

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

The topic ‘Multiple Loops not working’ is closed to new replies.

## Tags

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

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 2 participants
 * Last reply from: [Jacob Martella](https://wordpress.org/support/users/arenapigskin/)
 * Last activity: [8 years, 8 months ago](https://wordpress.org/support/topic/multiple-loops-not-working/#post-9580932)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
