Last active
November 8, 2022 19:34
-
-
Save siteoptimo/9049115 to your computer and use it in GitHub Desktop.
This will allow to order a wordpress user query by random.
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 | |
// put this in your functions.php | |
add_filter('pre_user_query', function(&$query) { | |
if($query->query_vars["orderby"] == 'rand') { | |
$query->query_orderby = 'ORDER by RAND()'; | |
} | |
}); | |
// Query will look like this: | |
$args = array('orderby' => 'rand', 'number' => 5); | |
$users = WP_User_Query($args); |
The anonymous function messed it up for me, I used:
function wp_user_query_random_enable($query) {
if($query->query_vars["orderby"] == 'rand') {
$query->query_orderby = 'ORDER by RAND()';
}
}
add_filter('pre_user_query', 'wp_user_query_random_enable');
Hi Guys, Thanks
Thank you much!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!