admin管理员组文章数量:1278886
So I'm trying to list my wordpress user id's each time in a random order.
Right now I have this, but for some reason, 'orderby' => 'rand'
is not randomizing the list.
<?php
$args = array(
'meta_key' => 'active',
'meta_value' => 'yes',
'number' => 99999,
'orderby' => 'rand',
);
$my_user_query = new WP_User_Query( $args );
$publishers = $my_user_query->get_results();
if ( ! empty( $publishers ) ): ?>
<?php foreach ( $publishers as $publisher ):
setup_postdata( $publisher )
?>
<?php echo $publisher->ID; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I have tried 'order' => 'rand'
which is not correct as to my knowledge.
To make sure that the above array works, I have tried:
'order' => 'ASC'
and 'order' => 'DESC'
both worked great.
I assume there's something wrong with my rand code line.
I have also heard that it's possible to use shuffle($users);
, but I have no idea where and how to implement.
Need help.
So I'm trying to list my wordpress user id's each time in a random order.
Right now I have this, but for some reason, 'orderby' => 'rand'
is not randomizing the list.
<?php
$args = array(
'meta_key' => 'active',
'meta_value' => 'yes',
'number' => 99999,
'orderby' => 'rand',
);
$my_user_query = new WP_User_Query( $args );
$publishers = $my_user_query->get_results();
if ( ! empty( $publishers ) ): ?>
<?php foreach ( $publishers as $publisher ):
setup_postdata( $publisher )
?>
<?php echo $publisher->ID; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I have tried 'order' => 'rand'
which is not correct as to my knowledge.
To make sure that the above array works, I have tried:
'order' => 'ASC'
and 'order' => 'DESC'
both worked great.
I assume there's something wrong with my rand code line.
I have also heard that it's possible to use shuffle($users);
, but I have no idea where and how to implement.
Need help.
Share Improve this question asked Sep 24, 2021 at 15:23 robert0robert0 2032 silver badges11 bronze badges 1- 1 note that asking the database to do the random sorting can be extremely expensive/slow as it can involve a full copy of the users table into a temporary table in memory that then gets shuffled before the full query can run. It's much faster to ask for the first entry after a randomly generated value produced in PHP. E.g. the first post published after a random date, or the 129th user, etc – Tom J Nowell ♦ Commented Sep 24, 2021 at 15:33
1 Answer
Reset to default 0Ok, so I found this solution in other thread which did worked:
Added this to functions.php:
add_action( "pre_user_query", function( $query ) {
if( "rand" == $query->query_vars["orderby"] ) {
$query->query_orderby = str_replace( "user_login", "RAND()", $query->query_orderby );
}
});
It sorts users by a random order now!
本文标签: phpOrder users by random not working
版权声明:本文标题:php - Order users by random not working? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741302145a2371149.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论