Created
November 10, 2012 17:20
-
-
Save mjangda/4051801 to your computer and use it in GitHub Desktop.
Randomize 100 most recent posts in PHP + a loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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