admin管理员组文章数量:1306862
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
.
本文标签: wp queryRandom users always showing same 8 users
版权声明:本文标题:wp query - Random users always showing same 8 users 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741801018a2398223.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论