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 *** */?>