Title: Recent Posts in Random Order
Last modified: August 19, 2016

---

# Recent Posts in Random Order

 *  Resolved [andy120](https://wordpress.org/support/users/andy120/)
 * (@andy120)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/recent-posts-random-order/)
 * I’m trying to put a list of recent posts in the sidebar of my blog, but I want
   them to be in random order. So the most recent 20 posts, but not ordered by date.
 *     ```
       <?php $topStoriesList = new WP_Query('showposts=20'); ?>
       <?php while ($topStoriesList->have_posts()) : $topStoriesList->the_post(); ?>
       <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
       <?php endwhile; ?>
       ```
   
 * This is what I have, but if I change it to “&orderby=rand” it randomizes the 
   posts it grabs so they aren’t the 20 most recent. Help? Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [tugbucket](https://wordpress.org/support/users/tugbucket/)
 * (@tugbucket)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/recent-posts-random-order/#post-1309151)
 * 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; ?>
       ```
   
 *  Thread Starter [andy120](https://wordpress.org/support/users/andy120/)
 * (@andy120)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/recent-posts-random-order/#post-1309156)
 * 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.
 *  [xdesi](https://wordpress.org/support/users/xdesi/)
 * (@xdesi)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/recent-posts-random-order/#post-1309169)
 * 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
 *  [tugbucket](https://wordpress.org/support/users/tugbucket/)
 * (@tugbucket)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/recent-posts-random-order/#post-1309170)
 * 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.
 *  Thread Starter [andy120](https://wordpress.org/support/users/andy120/)
 * (@andy120)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/recent-posts-random-order/#post-1309173)
 * tugbucket,
 * It works perfectly. Much appreciated!!
 * Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Recent Posts in Random Order’ is closed to new replies.

## Tags

 * [order](https://wordpress.org/support/topic-tag/order/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [recent](https://wordpress.org/support/topic-tag/recent/)

 * 5 replies
 * 3 participants
 * Last reply from: [andy120](https://wordpress.org/support/users/andy120/)
 * Last activity: [16 years, 5 months ago](https://wordpress.org/support/topic/recent-posts-random-order/#post-1309173)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
