i'm using "WP_User_Query" to displaying the most active user orderby = "post_count" in front and It works fine,

now i want to display the most active user based on comment count in front

i will be appreciate it if anyone could help me on this.

  • This is not possible via WP_User_Query on its own as it doesn't count comments in this query, therefore there is no easy orderby parameter to use. You may be able to work something out using something like this - ideally you would get the array of comments only once to build an order of most popular commenters (maybe using array_count_values)
    – Bysander
    Nov 23, 2020 at 17:16
  • @Bysander i understand what you mean but i don't think that i can make this work. could you please be more specific ? like a snippet or somthing else that could help me ...
    – Artan
    Nov 23, 2020 at 18:58
  • for anyone who may need this feature too : wordpress.stackexchange.com/questions/195155/… wordpress.stackexchange.com/questions/308517/…
    – Artan
    Nov 25, 2020 at 14:15

2 Answers 2


you can use this

$user = new wp_user(get_current_user_id());

function commentCount() {

global $user;
global $wpdb;
$count = $wpdb->get_var('SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' WHERE comment_author_email = "' . $user->data->user_email . '" and comment_approved = 1');
return $count;


echo '';

echo commentCount();

  • thanks , i already did it and it worked , but there is only one problem that still exist how can i get the count based on date ? For example most commenter in current month
    – Artan
    Nov 25, 2020 at 16:01

here is some code I have typed up straight from the codex - I have not tested or run it as I am at work but should put you on the right track. I have included all the pages to learn more about each function / step in the process. Parts of it have been deliberately left longer to help understand what's happening at each stage of this

// Find query params here
// https://developer.wordpress.org/reference/classes/wp_comment_query/__construct/
$args = array(
    'type'   => 'comment',
    'status' => 'approve',
// Get all approved comments
$comment_arr = get_comments( $args );

// Change array of comments into array of User IDs
// https://developer.wordpress.org/reference/functions/wp_list_pluck/
$list_users = wp_list_pluck( $comment_arr, 'user_id' );

// This will change the array of User IDs into another array this time containing the number of times a value appears
// https://www.php.net/manual/en/function.array-count-values.php
$totals = array_count_values( $list_users );

// Option one - loop through the totals array to get the user data from each user ID returned in the key
foreach ( $totals as $user => $n_of_comments ) {
    // https://developer.wordpress.org/reference/functions/get_userdata/
    $user_obj = get_userdata( $user );

// Option two
// - Flips the keys and values round ( https://www.php.net/manual/en/function.array-flip.php ) then
// Implode array to get a list of User IDs to pass to a single query ( https://www.php.net/manual/en/function.implode.php )
$list_in_order = implode( ',', array_flip( $totals ) );

$args  = array(
    'include' => $list_in_order,
    'orderby' => 'include',
$users = get_users( $args );
    foreach ( $users as $user ) {
    // code
  • thanks mate. option 2 works fine but the only problem is that the code shows all user in front . i put 'number' in option 2 array , it works but the user is not in the right order (user with the most comment). in Codex , i fount order by 'comment_count' but It does not work in either of array
    – Artan
    Nov 24, 2020 at 17:43
  • As mentioned - this code is written by eye - I don't have an easy means to test it. None of my sites allow comments so don't have any comment data to hand either. Having re-read the code I have updated the line with list pluck to use user_id which sounds more likely. You should try using some de-bugging along the code - things like print_r() or var_dump() will generally show you if you're getting back what you're expecting!
    – Bysander
    Nov 25, 2020 at 9:41
  • Still not working. But thank you anyway
    – Artan
    Nov 25, 2020 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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