Skip to content

Instantly share code, notes, and snippets.

@rfmeier
Created June 2, 2013 22:00
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 rfmeier/5695123 to your computer and use it in GitHub Desktop.
Save rfmeier/5695123 to your computer and use it in GitHub Desktop.
Display random posts on the home page in WordPress
<?php
add_action( 'pre_get_posts', 'display_random_posts' );
/**
* Callback for WordPress 'pre_get_posts' action.
*
* @link http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
* @author Ryan Meier <rfmeier@gmail.com>
*
* @param object $query The current query object
*/
function display_random_posts( $query ){
// check for main query and home page
if ( $query->is_home() && $query->is_main_query() ) {
// set the order to random
$query->set( 'orderby', 'rand' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment