Question

I am trying to display some random users on the page. It is changing the order of the users it is showing, but it is only showing the same users instead of randomizing from all users in the database.

So, it is currently showing 8 users, but always the same users, just in a different order. I have 100 users and it shouldn't always just show the same 8.

    <?php $args = array(
            'role__in' => array( 'member' ),
            'exclude' => array( 37, 1 ),
            'orderby' => 'rand'
          );
        $wp_user_query = new WP_User_Query($args);
        $members = $wp_user_query->get_results();
    ?>
Was it helpful?

Solution

Actually, rand is not a valid argument for orderby in the WP_User_Query class (it is in the WP-Query class). So, it should default to user_login, giving you an alphabetical list. Also, you should get all users that fit the member and exclude criteria from this query, not just eight.

This suggests there is other code interfering with yours.

My suggestion would be to start with adding 'number' => 100 to $args. That should overrule any other code limiting the amount of results. You could then use shuffle to randomize the array returned or pick some random elements from the array with array_rand.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top