Skip to content

Instantly share code, notes, and snippets.

@michaeldozark
Last active August 29, 2015 13:57
Show Gist options
  • Save michaeldozark/9839809 to your computer and use it in GitHub Desktop.
Save michaeldozark/9839809 to your computer and use it in GitHub Desktop.
Edit SQL ORDER BY statement to randomly order posts after other orders are accounted for.
<?php
/**
* slimline_random_orderby function
*
* Edit SQL ORDER BY statement to randomly order posts after other orders are accounted for.
*
* @param string $orderby The ORDER BY statement generated by get_posts()
* @return string $orderby ORDER BY statement with RAND() set as an additional clause
*/
function slimline_random_orderby( $orderby ) {
// Append SQL RAND function to an existing orderby statement or return by itself if $orderby is empty
return ( $orderby ? ',' : '' ) . 'RAND()';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment