$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to query 5 users in random who have published more than 10 posts|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to query 5 users in random who have published more than 10 posts

matteradmin9PV0评论

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.

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.

Share Improve this question asked Nov 6, 2018 at 14:00 p onelandp oneland 171 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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' );

Source

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

Post a comment

comment list (0)

  1. No comments so far