At the moment I am using wp_user_query to loop users from a specific role(leden) on a webpage. For the overview page this is working fine, but on my homepage I want to feature one random user.

I was a little bit astonished to find out there is no random order parameter, nor a parameter to put a limit on the items looped. The maximum items looped (similar to posts_per_page) is not the problem, i can make this work by putting a $i=1 and if($i == 1) variable in my loop to make it stop after one user.

But for the random function.. Am I overviewing something ? ... :/

Does anyone have any idea how to make this work nicely?

Thanks in advance and sorry for my bad english.

  • orderby' => 'rand' doesn't work?
    – timshutes
    Commented May 7, 2016 at 1:22
  • 1
    see this answer.
    – Milo
    Commented May 7, 2016 at 4:35
  • timshutes : nopes, only DESC and ASC are valid.. Dan: Just any user from a specific role :) @Milo: Thanks, seems like a nice solution. However, I think this will affect all userloops in the theme. But I think I found a solution, I'm gonna try it and than I'll come back to post it if it helped so others can use it too :)
    – Maartje
    Commented May 7, 2016 at 17:27

1 Answer 1


I found a realy great solition here. !!

It is a function that kindof 'adds' 'orderby' => 'rand' as a parameter. What it does it that when someone uses that parameter, the function will query the database using regular MySQL, where random is always possible :)

The function:

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

The loop will be something like:

// Query will look like this:
$args = array('orderby' => 'rand', 'number' => 5);

$users = WP_User_Query($args);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.