how to randomly list 5 authors with at least 3 published posts
-
06-11-2019 - |
Question
Bit of a newbie here...
As the title states, I am trying to generate a list of 5 random users (taken from authors, editors and administrators) who have published at least 3 posts each. (Also, I would like to exclude the author whose user ID is 4.)
I think I have managed to do all this (hacking together various solutions gratefully taken from useful previous posts here in wordpress.stackexchange!) but I am not sure if this is the best solution or if it's coded correctly. I use this feature on many pages throughout my website and am worried it might be too db query intensive.
<?php
global $wpdb;
$min_posts = 3;
$user_ids = $wpdb->get_col("
SELECT `post_author`
FROM
(SELECT `post_author`,
COUNT(*) AS `count`
FROM {$wpdb->posts}
WHERE `post_status`='publish'
GROUP BY `post_author`)
AS `stats`
WHERE `count` >= {$min_posts} AND post_author !=4;");
shuffle($user_ids);
for($someNumber = 1; $someNumber <= 5; $someNumber++) {
$user = get_userdata($user_ids[$someNumber]); ?>
<?php echo get_avatar( $user->ID, '60' );?>
<h7><a href="<?php echo get_author_posts_url( $user->ID ); ?>">
<?php echo $user->display_name ;?></a></h7>
<hr>
<?php
}
unset($user_ids);
?>
Any help truly appreciated, many thanks!
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange