Title: Problem with multiple loops
Last modified: August 20, 2016

---

# Problem with multiple loops

 *  Resolved [916VT](https://wordpress.org/support/users/vincent916/)
 * (@vincent916)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/problem-with-multiple-loops-3/)
 * Hi there,
    I’m trying to use the $do_not_duplicate on my single.php where I set
   two loops.
 * The strange point is : sometimes, instead of displaying 3 posts as resquested
   with posts_per_page=3, I only got 2.
 * I tried to set posts_per_page=2, and sometimes, it has displayed only 1.
 * Does anyone know where it can come from ?
 * Thanks a lot for helping 🙂
 *     ```
       <article class="single">
       		  <p class="page-title">Page1</p>
       		  <?php if (have_posts()) : while (have_posts()) : the_post(); $do_not_duplicate = $post->ID; ?>
       			  <div class="post">
       				<div class="post-info">
       				  	<h1 class="title"><?php the_title(); ?></h1>
       				</div>
       			  </div>
       			<?php endwhile; ?>
       		  <?php endif; ?>
       		</article>
   
       		<section class="related">
       		  <h2 class="title">Related</h2>
       		  <ul>
       			  <?php if(have_posts()) : ?>
       			  <?php query_posts('cat=9&showposts=3&orderby=rand'); ?>
       			  <?php while(have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
       			<li>
       			  <a href="<?php the_permalink();?>">
       				<span class="crp_title"><?php the_title(); ?></span>
       			  </a>
       			</li>
       			<?php endwhile; ?>
       			<?php endif;?>
       		  </ul>
       		</section><!-- End of Related-->
       ```
   

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/problem-with-multiple-loops-3/#post-3606099)
 * the Codex has an example which uses the query parameter ‘post__not_in’ instead
   just jumping over the duplicate post:
 * [http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action](http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action)
 * scroll down to ‘Note for Multiple Posts in the First Category’ and review what
   is shown there…
 *  Thread Starter [916VT](https://wordpress.org/support/users/vincent916/)
 * (@vincent916)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/problem-with-multiple-loops-3/#post-3606161)
 * Hi alchymyth,
    Thanks for your answer.
 * I tried to implement it but my first loop doesn’t use any query and I don’t how
   to change and make it works properly…
 * I guess I need to change that line :
 *     ```
       <?php if (have_posts()) : while (have_posts()) :
         the_post(); $do_not_duplicate = $post->ID; ?>
       ```
   
 * to that one :
 *     ```
       <?php $my_query = new WP_Query('category_name=featured&posts_per_page=2');
         while ($my_query->have_posts()) : $my_query->the_post();
         $do_not_duplicate[] = $post->ID ?>
       ```
   
 * but without query and args as it’s a template for single.php.
 * Can you give a hand ? 🙂
    Thanks a lot again !
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/problem-with-multiple-loops-3/#post-3606168)
 * the first loop stays unedited, just collects the post IDs in an array;
 * the second loop relly shoud be coded with `WP_Query()` and uses that array with
   the ‘post__not_in’ parameter;
 *     ```
       <article class="single">
       		  <p class="page-title">Page1</p>
       		  <?php $do_not_duplicate = array();
       if (have_posts()) : while (have_posts()) : the_post(); $do_not_duplicate[] = $post->ID; ?>
       			  <div class="post">
       				<div class="post-info">
       				  	<h1 class="title"><?php the_title(); ?></h1>
       				</div>
       			  </div>
       			<?php endwhile; ?>
       		  <?php endif; ?>
       		</article>
   
       		<section class="related">
       		  <h2 class="title">Related</h2>
       		  <ul>
       			  <?php $catloop = new WP_Query( array( 'cat' => 9, 'posts_per_page' => 3, 'orderby' => 'rand', 'post__not_in' => $do_not_duplicate ) );
       if($catloop->have_posts()) :
       			  while($catloop->have_posts()) : $catloop->the_post(); ?>
       			<li>
       			  <a href="<?php the_permalink();?>">
       				<span class="crp_title"><?php the_title(); ?></span>
       			  </a>
       			</li>
       			<?php endwhile; ?>
       			<?php endif; wp_reset_postdata(); ?>
       		  </ul>
       		</section><!-- End of Related-->
       ```
   
 * untested
 * [http://codex.wordpress.org/Class_Reference/WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query)
 *  Thread Starter [916VT](https://wordpress.org/support/users/vincent916/)
 * (@vincent916)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/problem-with-multiple-loops-3/#post-3606169)
 * It works, well, as my first code.
 * The number of elements displayed on .related is still, sometimes, one less than
   the posts_per_page…
 * For example, when I check a post, it displays three, but if I reload, only two.
   And could be the opposite.
 *  Thread Starter [916VT](https://wordpress.org/support/users/vincent916/)
 * (@vincent916)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/problem-with-multiple-loops-3/#post-3606172)
 * Actually, instead of sometimes, it’s more about 9 times on 10 that one element
   miss.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/problem-with-multiple-loops-3/#post-3606180)
 * can you post your currently used code?
 * please paste the full code of the template into a pastebin and post the link 
   to it here – [http://codex.wordpress.org/Forum_Welcome#Posting_Code](http://codex.wordpress.org/Forum_Welcome#Posting_Code)
 *  Thread Starter [916VT](https://wordpress.org/support/users/vincent916/)
 * (@vincent916)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/problem-with-multiple-loops-3/#post-3606181)
 * It’s embarrassing, your code works, mistake was mine, from another open tag on
   the template…
    I’m sorry about that and thank you again for your help ! 🙂

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

The topic ‘Problem with multiple loops’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 2 participants
 * Last reply from: [916VT](https://wordpress.org/support/users/vincent916/)
 * Last activity: [13 years, 1 month ago](https://wordpress.org/support/topic/problem-with-multiple-loops-3/#post-3606181)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
