Created
June 2, 2013 22:00
-
-
Save rfmeier/5695123 to your computer and use it in GitHub Desktop.
Display random posts on the home page in WordPress
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 | |
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 <[email protected]> | |
* | |
* @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