Skip to content

Instantly share code, notes, and snippets.

@mjangda
Created July 26, 2012 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjangda/3183616 to your computer and use it in GitHub Desktop.
Save mjangda/3183616 to your computer and use it in GitHub Desktop.
Shuffle a batch of posts in PHP instead of using orderby => rand with WP_Query
<?php
$testimonial = false;
$testimonials_query = new WP_Query( array( 'fields' => 'ids', 'posts_per_page' => 20 ) );
if ( ! empty ( $testimonials_query->posts ) ) {
$testimonials = $testimonials_query->posts;
shuffle( $testimonials );
$testimonial_id = array_shift( $testimonials );
$testimonial = get_post( $testimonial_id );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment