Multiple loops – values being passed to next loop?
-
Can anyone explain to me why values are being passed from the first loop to the second loop, specifically the array $loop2_output is getting values from the first loop?
<?php $first_loop = new WP_Query('showposts=2&cat=24&orderby=rand'); if ($first_loop->have_posts()) : while ($first_loop->have_posts()) : $first_loop->the_post(); $loop1_output[] = $post->ID; /* Creates array of loop 1 post IDs*/ ?> DO STUFF <?php endwhile; ?> <?php else : ?> <h2>You forgot to create any posts !</h2> <?php endif; ?> <?php $second_loop = new WP_Query('showposts=3&cat=23&orderby=rand'); if ($second_loop->have_posts()) : while ($second_loop->have_posts()) : $second_loop->the_post(); $loop2_output[] = $post->ID; /* Creates array of loop 2 post IDs*/ ?> DO STUFF <?php endwhile; ?> <?php else : ?> <h2>You forgot to create any posts !</h2> <?php endif; ?>
-
Any posts with both categories?
If the 2nd loop is excluding posts that showed in the first loop look at using the post__not_in argument in the 2nd query. See query_posts() for examples.
Thanks MichaelH
Yes, there are plenty of posts in both categories. This is the problem I think that maybe these are the posts being passed from one loop to the other. I was under the impression that new WP_Query completely reset everything so that this wasn’t possible.
I’ve kept my question here simple after tracking down what I think the problem is, but I’m excluding posts using the following:
<?php $second_loop = new WP_Query('showposts=2&cat=23&orderby=rand'); if ($second_loop->have_posts()) : while ($second_loop->have_posts()) : $second_loop->the_post(); $loop2_output[] = $post->ID; /* Creates array of everything output in loop 2*/ ?> <?php /* loop 1 didn't have a running total as it was the first loop. */ $running_total2 = array_merge ($loop1_output, $loop2_output); /* IDs of all posts in all loops outputted to date */ $running_total2 = array_unique($running_total2); /* remove duplicate entries */ ?> <?php if (!empty ($loop1_output)) /*make sure there are posts from previous loop*/ { if (in_array($post->ID, $loop1_output))continue;update_post_caches($posts); /* if first loop contained this post start on next iteration of Loop */ } ?>
But I don’t want to be sidetracked by the above code
This is easier for me to ‘fathom’ and loop2 doesn’t include posts from loop1.
<?php $args=array( 'category__in' => array(24), 'showposts'=>2, 'orderby' => 'rand' ); $first_loop = new WP_Query($args); if ($first_loop->have_posts()) : while ($first_loop->have_posts()) : $first_loop->the_post(); $loop1_output[] = $post->ID; /* Creates array of loop 1 post IDs*/ ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php endwhile; echo "<pre>"; print_r($loop1_output); echo "</pre>"; ?> <?php else : ?> <h2>You forgot to create any posts !</h2> <?php endif; ?> <?php $args=array( 'category__in' => array(24), 'post__not_in' => $loop1_output, 'showposts'=>3, 'orderby' => 'rand' ); $second_loop = new WP_Query($args); if ($second_loop->have_posts()) : while ($second_loop->have_posts()) : $second_loop->the_post(); $loop2_output[] = $post->ID; /* Creates array of loop 2 post IDs*/ ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php endwhile; echo "<pre>"; print_r($loop2_output); echo "</pre>"; ?> <?php else : ?> <h2>You forgot to create any posts !</h2> <?php endif; ?> ?>
Thanks Michael, you’ve come up trumps there but we must have crossed posts – I came up with the following after you pointed me in the right direction.
The following will extract random posts from category 1 in loop 1, and all remaining posts will be passed down to loop2.
Random posts will be extracted from category 2 in loop 2 (no duplicates from previous loop) and all remaining posts from loop 1 AND loop 2 will be passed down to loop 3.
Random posts will be extracted from category 3 in loop 3 (no duplicates from previous loops) and all remaining posts from loop 1 AND loop 2 AND loop 3 will be passed down to loop 4.Imagine a front page with a hundred posts on it – perhaps thumbnail images with text, maybe products. You want a random display of these products, so that users find the page constantly interesting. But you want your best selling products always at the top, and your less well-selling products further down, and your poorly selling products towards the bottom. You might only have three or four good selling products for the first year or two, and furthermore you’re constantly adding new products.
This code produces a cascading effect, with the probability of ‘good’ products in the top half of the page and ‘bad’ products in the bottom half.
All that needs to be done for each product/post is to assign it to categories 1,2,3,4 for best-selling products, 2.3.4 for good selling products, 3,4 for poorly selling products, 4 for very badly selling products.
<?/* ******* LOOP 1 ****** */?> <?php $loop1_output = array(); $first_loop = new WP_Query('showposts=20&cat=1&orderby=rand'); if ($first_loop->have_posts()) : while ($first_loop->have_posts()) : $first_loop->the_post(); $loop1_output[] = $post->ID; /* creates array of everything that was output in loop 1*/ /*OR use $loop1_output[] = get_the_ID();*/ ?> DO STUFF <?php endwhile; ?> <?php else : ?> <h2>You forgot to create any posts !</h2> <?php endif; ?> <p style="display:none;">Loop1 Checking output in source <?php asort($loop1_output); foreach ($loop1_output as $check1){ echo "$check1\n"; } ?> </p> <?/* ****** END OF LOOP 1 ***** */?> <?/* ****** LOOP 2 ******** */?> <?php $loop2_output = array(); $second_loop = new WP_Query(array('showposts' => 20,'cat' => 2,'post__not_in' => $loop1_output,'orderby' => rand,)); if ($second_loop->have_posts()) : while ($second_loop->have_posts()) : $second_loop->the_post(); $loop2_output[] = $post->ID; /*Creates array of everything output in loop 2*/ ?> <?php /* loop 1 didn't have a running total as it was the first loop. The running total will be used in all subsequent loops */ $running_total2 = array_merge ($loop1_output, $loop2_output); /* IDs of all postals in all loops outputted to date */ $running_total2 = array_unique($running_total2); /* remove duplicate entries */ ?> DO STUFF <?php endwhile; ?> <?php else : ?> <h2>You forgot to create any posts !</h2> <?php endif; ?> <p style="display:none;">loop2 Checking what output in Source code <?php asort($running_total2); foreach ($running_total2 as $check2){ echo "$check2\n"; } echo ('$loop_2 output'); print_r( $loop2_output); ?> </p> <?/* ******END OF LOOP 2 ****** */?> <?/* *** START LOOP 3 ** */?> <?php $loop3_output = array(); $third_loop = new WP_Query(array('showposts' => 20,'cat' => 3,'post__not_in' => $running_total2,'orderby' => rand,)); if ($third_loop->have_posts()) : while ($third_loop->have_posts()) : $third_loop->the_post(); $loop3_output[] = $post->ID; /*Creates array of everything output in loop 3*/ ?> <?php $running_total3 = array_merge ($loop3_output, $running_total2); /*IDs of all posts in all loops outputted to date*/ $running_total3 = array_unique($running_total3); /*remove duplicate entries*/ ?> DO STUFF <?php endwhile; ?> <?php else : ?> <h2 class="not-found">Oops! You forgot to create any posts !</h2> <?php endif; ?> <p style="display:none;">Loop3 Checking what is output in Source code <?php asort($running_total3); foreach ($running_total3 as $check3){ echo "$check3\n"; } echo ('$loop_3 output'); print_r( $loop3_output); ?> </p> <?/* **** END LOOP 3 **** */?> <?/* ***** START LOOP 4 ***** */?> <?php $loop4_output = array(); $fourth_loop = new WP_Query(array('showposts' => 20,'cat' => 4,'post__not_in' => $running_total3,'orderby' => rand,)); if ($fourth_loop->have_posts()) : while ($fourth_loop->have_posts()) : $fourth_loop->the_post(); $loop4_output[] = $post->ID; /*Creates array of everything output in loop 3*/ ?> <?php $running_total4 = array_merge ($loop4_output, $running_total3); /*IDs of all posts in all loops outputted to date*/ $running_total4 = array_unique($running_total4); /*remove duplicate entries*/ ?> DO STUFF <?php endwhile; ?> <?php else : ?> <h2 class="not-found">Oops! You forgot to create any posts !</h2> <?php endif; ?> <p style="display:none;">Loop4 Checking what is output in Source code <?php asort($running_total4); foreach ($running_total4 as $check4){ echo "$check4\n"; } echo ('$loop_4 output'); print_r( $loop4_output); ?> </p> <?/* **** END LOOP 4 *** */?>
If the posts in loop1 show never show in loops 2, 3 and 4, and posts in loop2 should never show in loops 3 and 4, couldn’t you just use one array, say $loop_output, and just keep assigning the ‘encountered’ posts from each loop to $loop_output, then just use $loop_output with post__not_in arguments?
So you wouldn’t need any of the $loopX_output or $running_totalX arrays.
Just a thought.
Possibly, though I’m not expert enough in php to understand ??
More to the point, the posts in loop 1 CAN show in loop 2, loop 3 etc. It’s fuzzy, it’s random, it’s what I want to happen.
The top posts will definitely be from my ‘best’ category, but as I go down the page the posts will usually come from my ‘worse’ categories – but NOT necessarily so.
I designed it like this because I didn’t like the look of it when loop 1 showed all the best posts / products, loop 2 showed the next best etc. – there was too clearly a demarcation. Plus I wanted to give the illusion of a constantly changing page to returning customers, though always displaying a selection of best products at the top.
Just pleased to have figure it out ??
- The topic ‘Multiple loops – values being passed to next loop?’ is closed to new replies.