Title: Random post selection
Last modified: December 29, 2018

---

# Random post selection

 *  [Dick Wynne](https://wordpress.org/support/users/lodestar/)
 * (@lodestar)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/random-post-selection/)
 * Most online search responses to this requirement show how to randomize the selection
   _ordering_ only, not the _selection_ itself. I wanted a solution not tied to 
   using a widget or shortcode, but which I could use in any context. Solution below
   for your child theme’s functions.php. You can see it (at the time of writing)
   working on my homepage at lodestarbooks.com, where typing 10403 as the number
   of posts in my Divi blog grid gives me 4 posts at random, with an offset of 3.
   This might get slow with a very large number of matching posts, but I doubt my
   site will hit that problem. A better high-volume solution would involve selecting
   only the required number of posts randomly at the outset — non-trivial, but I
   might get there if you don’t first.
 *     ```
       /* Select random posts by selecting all matching ones, applying the offset if any,
           shuffling the remainder, then slicing off the required number;
           randomness is requested by the posts_per_page being 5 digits long,
           where digits 2 and 3 are the number of posts required and the rightmost two
           digits are the offset; eg 10403 gets 4 posts randomly, with an offset of 3.
           This *may* get slow where the number of matching posts is very large */
       add_action( 'pre_get_posts', function( $query )
       {
           $actionCode = (int) $query->get('posts_per_page');
           if ( $actionCode > 1000 ) {
               $query->set('_randomize', $actionCode % 1000);
               $query->set('posts_per_page', -1);
           }
       }, 10, 1 );
       add_filter( 'the_posts', function( $posts, $query )
       {
           if( $actionCode = (int) $query->get( '_randomize' ) )
           {
               $no_posts = (int) ($actionCode / 100);
               $offset = $actionCode % 100;
               $posts = array_slice( $posts, $offset );
               shuffle( $posts );
               $posts = array_slice( $posts, 0, $no_posts );
           }
           return $posts;
       }, 10, 2 );
       ```
   
    -  This topic was modified 7 years, 5 months ago by [Dick Wynne](https://wordpress.org/support/users/lodestar/).
    -  This topic was modified 7 years, 5 months ago by [Dick Wynne](https://wordpress.org/support/users/lodestar/).

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

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/random-post-selection/#post-11035769)
 * What is your question? (This is a support forum, not a blog.)
    There are a LOT
   of answers on the internet for “How to do random”. Your way is expensive, since
   it retrieves all posts.
 *  Thread Starter [Dick Wynne](https://wordpress.org/support/users/lodestar/)
 * (@lodestar)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/random-post-selection/#post-11035841)
 * If this is just a support mechanism why is it a public forum? Presumably so people
   can find solutions. I provided one to a very common request. There are no answers
   I could find online which do not mis-interpret the problem (by just re-ordering
   a post selection) or assume you want a widget or a shortcode (which I don’t).
   As I have acknowledged, this solution will be inefficient for a large number 
   of matching posts, but I will have only a few dozen. Not sure what’s wrong with
   this, and presumably a moderator agreed since the post was moderated.
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/random-post-selection/#post-11035899)
 * > If this is just a support mechanism why is it a public forum?
 * Because anyone can ask and anyone can answer.
 * >  I provided one to a very common request.
 * If you had posted it on the actual request, that would have made more sense.
 * >  (by just re-ordering a post selection)
 * Isn’t that what `shuffle` does?
 *  Thread Starter [Dick Wynne](https://wordpress.org/support/users/lodestar/)
 * (@lodestar)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/random-post-selection/#post-11036043)
 * Forgive me for missing that I could classify this as not a support question, 
   which I have now done. I can’t make the rationale for my suggested solution any
   clearer, I hope others may recognise it.

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

The topic ‘Random post selection’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 2 participants
 * Last reply from: [Dick Wynne](https://wordpress.org/support/users/lodestar/)
 * Last activity: [7 years, 5 months ago](https://wordpress.org/support/topic/random-post-selection/#post-11036043)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
