Skip to content

Instantly share code, notes, and snippets.

@mjangda
Created November 10, 2012 17:20
Show Gist options
  • Save mjangda/4051801 to your computer and use it in GitHub Desktop.
Save mjangda/4051801 to your computer and use it in GitHub Desktop.
Randomize 100 most recent posts in PHP + a loop
<?php $query = new WP_Query( array( 'posts_per_page' => 100, 'fields' => 'ids' ) ); ?>
<?php if ( $query->have_posts() ) :
$post_ids = $query->posts;
shuffle( $post_ids );
$post_ids = array_splice( $post_ids, 0, 12 );
foreach ( $post_ids as $post_id ) :
$post = get_post( $post_id );
setup_postdata( $post );
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endforeach; ?>
<?php endif; wp_reset_postdata(); ?>
@KingYes
Copy link

KingYes commented Apr 9, 2014

Why you don't use with rand() MySQL method?
For more info from WP Codex: http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment