I want to display 5 users who have published more than 10 posts, when the page loaded again, it should show another 5 users or mixed with new users with more than 10 posts.

How do I achieve this? Thank you for any help.

1 Answer 1


I tried this code and it should work for you

In your function.php to make random order available in WP_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;
add_action( 'pre_user_query', 'my_random_user_query' );


And a query like this

// Get all users id
$args = array (
 'has_published_posts' => true,
 'fields' => 'ids',
$wp_users = new WP_User_Query( $args );
$ids_users = $wp_users->get_results();

// Return users id with more than 10 posts
$users = array_filter($ids_users, function($id){
  $count = count_user_posts( $id );
  if($count >= 10) return $id;

// Get users previously filtered 
$args = array (
 'orderby'    => 'rand',
 'include'    => $users,
 'number' => 5
$wp_users = new WP_User_Query( $args );
$result = $wp_users->get_results();

Codex query about users

  • Hey, thank you so much. You just saved my ass, bro. By the way, add_action( 'pre_user_query', 'my_random_user_query' ); should be added to the above function lines.
    – p oneland
    Commented Nov 6, 2018 at 16:17
  • Then I have a custom meta for users simple_local_avatar. How do I filter (display) the users who have uploaded local avatar ? I'll appreciate so much if you can help with this.
    – p oneland
    Commented Nov 6, 2018 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.