Title: orderby=rand
Last modified: August 19, 2016

---

# orderby=rand

 *  [nerdynothings](https://wordpress.org/support/users/nerdynothings/)
 * (@nerdynothings)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/orderbyrand/)
 * I have a featured post at the top of my site: [http://nerdynothings.com](http://nerdynothings.com)
 * I would like that post to be picked at random from the 10 most recent posts. 
   Is this possible using “orderby=rand”. Can i pass parameters to it, or is there
   another solution?
 * Thanks!!

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

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/orderbyrand/#post-1603701)
 * If you use ‘orderby=rand’, it overrides ‘most recent’, so that will not work.
   You can use [get_col](http://codex.wordpress.org/Function_Reference/wpdb_Class#SELECT_a_Column)
   to select the IDs of the 10 most recent posts into an array. Then pick one of
   the 10 at random to retrieve for the featured post.
 *  Thread Starter [nerdynothings](https://wordpress.org/support/users/nerdynothings/)
 * (@nerdynothings)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/orderbyrand/#post-1603727)
 * that will absolutely work. but is far too advanced for me. I don’t know how to
   set the parameters of the Database query, such as SELECT, FROM and WHERE…
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/orderbyrand/#post-1603744)
 * Looks like you are using a custom theme, so I can’t download it to look at the
   code. If you post your index.php file in the [pastebin](http://wordpress.pastebin.com),
   I will look at it to see if I can give more specific directions.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/orderbyrand/#post-1603779)
 * Below is a code sample that selects a random post and echoes the title, date,
   and ID. You should replace the echo with the code from your template that displays
   the featured post.
 *     ```
       <?php
       // Select a random post from the x most recent posts
       $number_of_posts = 10;
       $sql = "SELECT ID FROM $wpdb->posts
          WHERE post_type = 'post'
             AND post_status = 'publish'
             AND post_date <= NOW()
          ORDER BY post_date DESC
          LIMIT 0,$number_of_posts";
       $ids = $wpdb->get_col($sql);
       $randid = $ids[rand(0,sizeof($ids) - 1)];
       $posts = get_posts("p=$randid");
       if ($posts) {
         $post = $posts[0];
         setup_postdata($post);
         // echo '<p>POST:';print_r($post);echo '</p>';
         echo "<p>RANDOM TITLE:".get_the_title() . "  DATE:$post->post_date  ID:$post->ID</p>";
       }
       ?>
       ```
   

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

The topic ‘orderby=rand’ is closed to new replies.

## Tags

 * [featured](https://wordpress.org/support/topic-tag/featured/)
 * [post](https://wordpress.org/support/topic-tag/post/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [15 years, 9 months ago](https://wordpress.org/support/topic/orderbyrand/#post-1603779)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
