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();
?>
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();
?>
Share
Improve this question
edited Jan 27, 2021 at 10:11
cjbj
15k16 gold badges42 silver badges89 bronze badges
asked Jan 27, 2021 at 8:26
user8463989user8463989
5931 gold badge8 silver badges24 bronze badges
1
- You might be better to get all users in a single query and then randomly take X from that array. – Q Studio Commented Jan 27, 2021 at 9:42
1 Answer
Reset to default 0Actually, 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
.