Title: random posts for custom select query?
Last modified: August 19, 2016

---

# random posts for custom select query?

 *  [janieavis](https://wordpress.org/support/users/janieavis/)
 * (@janieavis)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/random-posts-for-custom-select-query/)
 * Hi,
 * I have set up a custom select query using the following codex doc and it works
   well – it uses setup_postdata($post) to get posts:
    [http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query](http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query)
 * One thing I would like to do is display only one random post, but I have tried
   `
   <?php query_posts('numberposts=1&orderby=rand'); ?>` and
 *     ```
       <?php
        $rand_posts = get_posts('numberposts=1&orderby=rand');
        foreach( $rand_posts as $post ) :
        ?>
       ```
   
 * but neither seem to do the trick.
    Any ideas how to incorporate this functionality
   into the code:
 *     ```
       <?php if ($pageposts): ?>
        <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post); ?>
       ```
   
 * Thanks,

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/random-posts-for-custom-select-query/#post-1195427)
 *     ```
       <?php
       $rand_posts = get_posts('showposts=1&orderby=rand');
       echo "<pre>"; print_r($rand_posts); echo "</pre>";
       ?>
       ```
   
 * Works fine for me.
 * Now just [need a loop](http://codex.wordpress.org/The_Loop) to display those 
   posts. Once you read about The Loop, then that should also answer your 2nd question.
 *  Thread Starter [janieavis](https://wordpress.org/support/users/janieavis/)
 * (@janieavis)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/random-posts-for-custom-select-query/#post-1195607)
 * Thanks Michael! but I am not sure how to integrate this into the above code because
   this functionality does not seem to use the standard loop.
 * My understanding about this functionality is that a certain group of posts are
   selected and put in $posts & then it uses setup_postdata instead of the regular
   get_posts function to display them.
 *     ```
       <?php if ($pageposts): ?>
        <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post); ?>
       ```
   
 * If I tack your code onto the end of this code it displays any random post from
   the entire database but I am trying to get a random post only from those contained
   in $post.
 * Do you have any ideas how to make this work?
    Thanks! janie
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/random-posts-for-custom-select-query/#post-1195655)
 * Some other loop examples:
 * [http://www.daobydesign.com/blog/2007/07/modifying-wordpress-front-page/](http://www.daobydesign.com/blog/2007/07/modifying-wordpress-front-page/)

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

The topic ‘random posts for custom select query?’ is closed to new replies.

 * 3 replies
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [16 years, 9 months ago](https://wordpress.org/support/topic/random-posts-for-custom-select-query/#post-1195655)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
