WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Array Problem with Multiple Loops on the same page (10 posts)

  1. jitpal
    Member
    Posted 5 years ago #

    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.

  2. t31os
    Member
    Posted 5 years ago #

    add...

    wp_reset_query();

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

  3. jitpal
    Member
    Posted 5 years ago #

    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.

  4. t31os
    Member
    Posted 5 years ago #

    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.. :)

  5. jitpal
    Member
    Posted 5 years ago #

    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

  6. jitpal
    Member
    Posted 5 years ago #

    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

  7. t31os
    Member
    Posted 5 years ago #

    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

  8. jitpal
    Member
    Posted 5 years ago #

    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.

  9. jitpal
    Member
    Posted 5 years ago #

    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

  10. jitpal
    Member
    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic