try adding the orderby=rand
property like:
<?php $topStoriesList = new WP_Query('showposts=20&orderby=rand'); ?>
<?php while ($topStoriesList->have_posts()) : $topStoriesList->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?>
I just copy/pasted that and tried it, but the results were completely random regardless of whether they were the most recent 20 or not.
You could probably use shuffle($post-array);
But not too sure on the name of the array of which retrieved posts from the query are stored in.
EDIT: Try adding shuffle($posts) after the if have_posts part of your loop
okay how about this. maybe a little long winded but seems to do the trick.
<?php
$number = "4";
$posts = "SELECT * from $wpdb->posts WHERE post_type='post' ORDER BY post_date DESC LIMIT $number";
$postX = array();
$postresult = mysql_query($posts) or die(mysql_error());
while ($row = mysql_fetch_array($postresult)) {
$postX[] = $row[0];
}
//$ids = shuffle($postX);
$ids = implode(',', $postX);
//echo $ids;
?>
<?php
$args = array(
'showposts'=> $number,
'post__in' => explode(',', $ids),
'orderby' => 'rand'
);
query_posts($args);
?>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?>
See at the top of the code where it has $number = "4";
? change the “4” to be the number you want to show.
tugbucket,
It works perfectly. Much appreciated!!
Thanks.