Home > other >  Showing only certain amount of users
Showing only certain amount of users

Time:02-23

On my site I am showing all users with their profile picture that have subscribed to the site (using Wordpress/PHP). I am struggling to only show 20 random that have signed up tho, could anyone please advise?

My code is as follows:-

    <?php
    $args = array(
        'role'    => 'subscriber',
        'orderby' => 'user_nicename',
        'order'   => 'ASC',
        'posts_per_page' => 20,
    );
    $users = get_users( $args );

    echo '<ul >';
    foreach ( $users as $user ) {echo '<li>' . get_avatar( $user->user_email , 96 ) . '</li>';
    }
    echo '</ul>';

    ?>  

Thank you

CodePudding user response:

You need to add a action hook in function.php then you can use below mentioned code to register order by random.

add_action( 'pre_user_query', 'my_random_user_query' );

function my_random_user_query( $class ) {
    if( 'rand' == $class->query_vars['orderby'] )
        $class->query_orderby = str_replace( 'user_login', 'RAND()', $class->query_orderby );

    return $class;
}

Then you can define your arguments like this.

$args = array(
     'role'    => 'subscriber',
     'orderby' => 'rand',
     'order'   => 'DESC',
     'posts_per_page' => 20,
);

CodePudding user response:

My understanding is you're not attempting to show the users in random order, rather trying to get 20 random users to show. If that's the case, you likely have to run two queries.

First, you would need to get all the IDs for the Subscriber role. Then create an array of random IDs from that initial returned array from the first query. Something like this:

First part: Get the Subscriber IDs and create an array of 20 random IDs

<?php

// First query includes grabbing all IDs from Subscriber role

$args = array(
  'role' => 'subscriber',
  'fields' => 'ID' // Use the fields param since all we need to return right now is IDs
);

$user_ids = get_users( $args ); // This will return an array of user IDs

$random_ids = array_rand( $user_ids, 20 ); // We'll create a new array of 20 random user IDs 

Second part: Run another query for user data, using the random IDs we just generated

// Create a new query to get data for the 20 random IDs 
// Note that the role param is removed since we already queried that

$args = array(
  'orderby' => 'user_nicename',
  'order' => 'ASC',
  'include' => $random_ids // Only get data for the IDs that are in our array or random user IDs
);

// Get your data and output it

$random_users = get_users( $args ); ?>

Third part: Output your data how you want

<ul >

<?php 

foreach ($random_users as $user) { ?>

  <li><?php get_avatar( $user->user_email, 96); ?></li>
  
<?php } ?>

</ul>

Here it is all together

<?php

// First query includes grabbing all IDs from Subscriber role

$args = array(
  'role' => 'subscriber',
  'fields' => 'ID'
);

$user_ids = get_users( $args ); // This will return an array of user IDs

$random_ids = array_rand( $user_ids, 20 ); // We'll create a new array of 20 random user IDs 

// Create a new query to get data for the 20 random IDs 

$args = array(
  'orderby' => 'user_nicename',
  'order' => 'ASC',
  'include' => $random_ids
);

// Get your data and output it

$random_users = get_users( $args ); ?>

<ul >

<?php 

foreach ($random_users as $user) { ?>

  <li><?php get_avatar( $user->user_email, 96); ?></li>
  
<?php } ?>

</ul>
  • Related