I'm trying to setup a shortcode to show a single random user each time the page is refreshed. I've been successful in displaying all users or a select user by the user ID.

Here is my shortcode as it is now:

//* Shortcode for getting users
function list_of_users( $atts ) {

    extract( shortcode_atts(
            'display' => 'all',
            'user' => '30'

    switch ( $display ) {
        case 'all':
            $content = display_all_users();
        case 'single':
            $content = display_single_user( (int) $user );
        case 'rotate':
            $content = display_rotate_users();
    return $content;
add_shortcode('staff', 'list_of_users');

Here's the function to display all users:

function display_all_users(){ 
    $args = array(
        'orderby' => 'ID',
        'order' => 'ASC'
    $users = get_users( $args );

    $html = '<ul class="staff">';
        foreach( $users as $user ){ 
            $user_info = get_userdata($user->ID);
            $html .= '<li>';
            $html .= '<a href="'.get_home_url().'/author/'.$user_info->user_nicename.'" class="staff-image">';
            $html .= mt_profile_img( $user->ID, array('size' => '250x250','echo' => false));
            $html .= '</a>';
            $html .= '<div class="staff-info"><a href="'.get_home_url().'/author/'.$user_info->user_nicename.'" class="staff-name"><h2>'.$user->display_name.'</h2></a>';
            $html .= '<div class="service-certs">';
            $html .= get_field('certifications','user_'.$user->ID);
            $html .= '</div>';
            $html .= '<p class="service-excerpt">';
            $html .= get_field('short_bio','user_'.$user->ID);
            $html .= '</p>';
            $html .= '<a href="'.get_home_url().'/author/'.$user_info->user_nicename.'" class="more-staff-bio">Read more from ' . $user_info->user_firstname . '</a></div><div style="clear: both;"></div>';
            $html .= '</li>';
    $html .= '</ul>';
    return $html;

Here's the function to show a single user by ID:

function display_single_user( $user_id = 30 ){

    $html = '<div class="home-profile-image">';
    $html .=  mt_profile_img( $user_id, array('size' => '175x175','echo' => false));
    $html .= '</div><div class="home-short-bio">';
    $html .=  get_field('short_bio','user_'.$user_id);
    $html .= '</div><a href="'.get_home_url().'/our-team/" class="button">Read more staff bios</a>';

    return $html;

Where I'm stuck is to take the list of all users and then randomizing the user id to then use to display certain info. I would like to keep the output of the single user (above) for this random user...it's just a matter of how to get that random ID.

Any suggestions on how to accomplish this?

  • Pretty simple. Modify your existing code and instead of 'orderby' => 'ID' you should be able to do 'orderby' => 'rand'.
    – GhostToast
    Commented Dec 19, 2013 at 17:42
  • @GhostToast That was my original thought, however, the WP Codex leads me to believe that's not an option: codex.wordpress.org/Function_Reference/get_users
    – NW Tech
    Commented Dec 19, 2013 at 17:47
  • Plz pals am stil young in PHP and neither a pro in Wordpress. This is exactly what I have been looking for bt if am to use any of this examples, where do I "paste" it in my wordpress site?
    – user45247
    Commented Jan 16, 2014 at 15:15
  • My example is for a shortcode that I built. So everything got added to my theme's function.php so that I was able to use my shortcode in the page/post editor
    – NW Tech
    Commented Jan 16, 2014 at 16:29

2 Answers 2


By default WordPress allow only order ASC and DESC. However, you can use WP_User_Query and the action pre_user_query to adjust the query (that is passed by reference) just like you want.

This is efficient because you get only one user, not all.

function my_user_by_rand( $ua ) {
  // remove the action to run only once
  remove_action('pre_user_query', 'my_user_rand');
  // adjust the query to use random order
  $ua->query_orderby = str_replace( 'user_login ASC', 'RAND()', $ua->query_orderby );

function display_random_user(){
  // add the filter
  add_action('pre_user_query', 'my_user_by_rand');
  // setup the query
  $args = array(
    'orderby' => 'user_login', 'order' => 'ASC', 'number' => 1
  $user_query = new WP_User_Query( $args );
  // run the query
  // get the result
  $random = ! empty($user_query->results) ? array_pop($user_query->results) : FALSE;
  // just for debug

Something like this should get you a random user. Not tested.

function display_random_user(){ 
    $users = get_users();
    $user_info = get_userdata($users[array_rand($users)]->ID);
    // do stuff with info

PHP: array_rand - Manual

  • Thanks @GhostToast. I found another option that involved something like shuffle($users);array_rand($users);foreach( $users as $user ){ //do stuff }unset($uid,$userID); ...it seemed to work as well. Do you have any insight on which is a better solution?
    – NW Tech
    Commented Dec 19, 2013 at 18:01
  • well, foreach would imply you're doing this to more than one $user. We really just want to randomly select one from the deck and be done. You could still shuffle() before getting your array_rand(), which help to make the randomization more random probably.
    – GhostToast
    Commented Dec 19, 2013 at 18:06
  • Thanks. If there's no benefit to one way or the other, I'll stick with your solution...it seems more efficient.
    – NW Tech
    Commented Dec 19, 2013 at 18:20

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.