Random users always showing same 8 users

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();
    ?>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x