Skip to content

Instantly share code, notes, and snippets.

@siteoptimo
Last active November 8, 2022 19:34
Show Gist options
  • Save siteoptimo/9049115 to your computer and use it in GitHub Desktop.
Save siteoptimo/9049115 to your computer and use it in GitHub Desktop.
This will allow to order a wordpress user query by random.
<?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);
@patrick-wc
Copy link

Thank you!

@patrick-wc
Copy link

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');

@Gori4ka
Copy link

Gori4ka commented Nov 29, 2018

Hi Guys, Thanks

@SeanTOSCD
Copy link

Thank you much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment