Support » Fixing WordPress » Array Problem with Multiple Loops on the same page

  • Resolved jitpal


    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/ 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)
  • add…


    After the end of each query and see if that helps..

    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.

    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.. 🙂

    So I started playing around with this other method of avoiding duplicate posts. Alternate Method. 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

    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

    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…

    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.

    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

    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();
       'post__in' => $sticky,
    	while (have_posts()) : the_post();  ?>
    <!--DO STUFF-->
    <?php $ids[]= $post->ID; endwhile; wp_reset_query(); ?>

    Second Loop:

       'post__not_in' => $sticky,
    	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.