Title: Array Problem with Multiple Loops on the same page
Last modified: August 19, 2016

---

# Array Problem with Multiple Loops on the same page

 *  Resolved [jitpal](https://wordpress.org/support/users/jitpal/)
 * (@jitpal)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/array-problem-with-multiple-loops-on-the-same-page/)
 * I was following the codex entry on doing multiple loops on the same page. I have
   both loops going but my problem is that my first loop is multiple posts and my
   2nd loop is multiple posts, thus I need to do the “$do_not_duplicate” part as
   an array however when I follow the codex thing i get this.
    `atal error: Cannot
   use [] for reading in /nfs/c02/h04/mnt/25450/domains/pensfast.com/html/wp-content/
   themes/prebuilt/sliders/sale_slider.php on line 4`
 * Any ideas why this is happening and how I can fix this? Here are the snippets
   of code from the two loops.
 * The first one:
 *     ```
       <?php $my_query = new WP_Query('is_sticky');
         while (have_posts()) : the_post();
         if( $post->ID = $do_not_duplicate[] ) continue; update_post_caches($posts); ?>
       ```
   
 * The second one:
 *     ```
       <?php $my_query = new WP_Query('showposts=4') ?>
       <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
       	if($post->ID == $do_not_duplicate[0] || $post->ID == $do_not_duplicate[1] || $post->ID == $do_not_duplicate[2]) continue; update_post_caches($posts); ?>
       ```
   
 * Also the other thing that was stumping me and I’m not sure if this is related,
   but when I do the showposts thing in the query, it doesn’t show the amount I 
   put, it’s always more. If I put 1 for example, it’ll show 8. Thanks.

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

 *  [t31os](https://wordpress.org/support/users/t31os/)
 * (@t31os)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/array-problem-with-multiple-loops-on-the-same-page/#post-1033223)
 * add…
 * `wp_reset_query();`
 * After the end of each query and see if that helps..
 *  Thread Starter [jitpal](https://wordpress.org/support/users/jitpal/)
 * (@jitpal)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/array-problem-with-multiple-loops-on-the-same-page/#post-1033270)
 * That did nothing except mess up what was already showing up when I remove the
   do_not_duplicate part. Maybe I put it in the wrong spot. I put it after the first
   and second loop directly after endwhile. Thanks.
 *  [t31os](https://wordpress.org/support/users/t31os/)
 * (@t31os)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/array-problem-with-multiple-loops-on-the-same-page/#post-1033326)
 * After the endif of ..
 * if($myquery->have_posts()) :
 * is where i place mine…
 * Can i assume that when you remove one of those loops it functions as you expect,
   with exception of course to the fact that one query is missing.. 🙂
 *  Thread Starter [jitpal](https://wordpress.org/support/users/jitpal/)
 * (@jitpal)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/array-problem-with-multiple-loops-on-the-same-page/#post-1033338)
 * So I started playing around with this other method of avoiding duplicate posts.
   [Alternate Method](http://www.goboxy.com/wordpress/avoid-duplicate-posts-in-multiple-loops/).
   Which doesn’t cause an error but it doesn’t show the 2nd loop. I think what happens
   is that my first query doesn’t only return the sticky posts(it does show the 
   right ones though) but it returns all of them. So basically the 2nd part then
   filters out all of the posts. So I tried the reset query thing, which didn’t 
   do anything.
 * As for functioning it looks like it does, but the problem with the showposts 
   is still there. Thanks
 *  Thread Starter [jitpal](https://wordpress.org/support/users/jitpal/)
 * (@jitpal)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/array-problem-with-multiple-loops-on-the-same-page/#post-1033342)
 * Alright so I think my suspicion is correct. I tried a different query where I
   just told it find all posts with a specific tag. The query returned the correct
   results and then the 2nd loop excluded it. So now my problem is to make the first
   loop show only sticky posts and the second loop to show the 6 newest posts without
   duplicating what’s on the top. Of course the showposts not showing the right 
   number of posts still. Thanks
 *  [t31os](https://wordpress.org/support/users/t31os/)
 * (@t31os)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/array-problem-with-multiple-loops-on-the-same-page/#post-1033388)
 * Ok, just so i’m clear on what you want…
 * You’re trying to grab X amount of stickie posts, then after that (and seperately)
   grab another X amount of posts, which must not match any of the stickies.
 * To add:: Had a look at that link you posted, that’s exactly how i’d do it, create
   a query, store the results in an array, then use that array to exclude the posts
   for the next query…
 * I’m not sure that’s the best approach.. since you need to keep the old query 
   around for the second (hence the issue when using reset_query)… there must be
   an easier way, i’ll have a shot on my local install tomorrow..
 * However in the mean time, i think you can do it by including stickies on the 
   first query, then excluding them on the second, and you’ll be able to clear/reset
   after each query…
 * [http://codex.wordpress.org/Template_Tags/query_posts#Sticky_Post_Parameters](http://codex.wordpress.org/Template_Tags/query_posts#Sticky_Post_Parameters)
 *  Thread Starter [jitpal](https://wordpress.org/support/users/jitpal/)
 * (@jitpal)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/array-problem-with-multiple-loops-on-the-same-page/#post-1033404)
 * You are right on what I want. That’s exactly it. For some reason now, the query_posts
   for sticky posts seems ot have no effect on the loop. If I change query posts
   from sticky to something else it works fine. I think I’m going to let it sit 
   until morning and then try and figure it out. I appreciate your help today. Thanks.
 *  Thread Starter [jitpal](https://wordpress.org/support/users/jitpal/)
 * (@jitpal)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/array-problem-with-multiple-loops-on-the-same-page/#post-1033495)
 * I think something is terribly wrong. When I tell it to exclude sticky posts in
   the second loop, it just posts all of them in the order they were posted but 
   it doesn’t exclude the sticky posts. Thanks
 *  Thread Starter [jitpal](https://wordpress.org/support/users/jitpal/)
 * (@jitpal)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/array-problem-with-multiple-loops-on-the-same-page/#post-1033498)
 * Nevermind. Disregard my last post. Alright I got this working. I used the alternate
   method of not duplicating posts. I’ll post my code here just in case someone 
   needs to reference something like this in the future.
 * First Loop:
 *     ```
       <?php $ids = array();
       	$sticky=get_option('sticky_posts');
       	$args=array(
          'showposts'=>6,
          'post__in' => $sticky,
          );
       query_posts($args);
       	while (have_posts()) : the_post();  ?>
       <!--DO STUFF-->
       <?php $ids[]= $post->ID; endwhile; wp_reset_query(); ?>
       ```
   
 * Second Loop:
 *     ```
       <?php
       	$sticky=get_option('sticky_posts');
       	$args=array(
          'showposts'=>6,
          'post__not_in' => $sticky,
          );
       query_posts($args);
       	while (have_posts()) : the_post();  ?>
       	<?php if (!in_array($post->ID, $ids)) { ?>
       <!--DO STUFF-->
        <?php } endwhile; ?>
       ```
   
 * I think it’s working right, I’ll try and break it to see if it keeps working 
   but I think it’s good. Even my showposts problem from before seems to be solved.
   Thanks for all of your help t31os.

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

The topic ‘Array Problem with Multiple Loops on the same page’ is closed to new 
replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 2 participants
 * Last reply from: [jitpal](https://wordpress.org/support/users/jitpal/)
 * Last activity: [17 years, 2 months ago](https://wordpress.org/support/topic/array-problem-with-multiple-loops-on-the-same-page/#post-1033498)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
