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
Add a comment  | 

1 Answer 1

Reset to default 0

Ok, 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