Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.