1

how can I display a list of the most active users (with permalink and thumb) sort by comments count?

I found this on WP Codex that list all the users, but I don't know how to sort by comment count.

<?php
$args = array(
    'number' => 10,
    'orderby' => name
);

// The Query
$user_query = new WP_User_Query( $args );

// User Loop
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        echo '<p>' . $user->display_name . '</p>';
    }
} else {
    echo 'No users found.';
}
?>

Thank you very much in advance

EDIT

I found this code on the web:

<?php
global $wpdb;
$where = 'WHERE comment_approved = 1 AND user_id <> 0';
$comment_counts = (array) $wpdb->get_results("
        SELECT user_id, COUNT( * ) AS total
        FROM {$wpdb->comments}
        {$where}
        GROUP BY user_id
    ", object);

    echo '<ul>';

    foreach ( $comment_counts as $count ) {
      $user = get_userdata($count->user_id);
    //echo "<pre>"; print_r($count); echo "</pre>";
    //echo "<pre>"; print_r($user); echo "</pre>";
      echo '<li>User ' . get_avatar( $user->ID ) . "<a href='" . get_author_posts_url($user->ID) . "'>" . $user->display_name . "</a>" . ' comment count is ' . $count->total . "</li>" . '
    ';
    }
    echo '</ul>';
    ?>

I added the author link and the thumb and it seems it works. Do you think it is correct get_avatar( $user->ID ) and get_author_posts_url($user->ID)?

But the problem is always the same.. How can I sort the authors list by comment count?

EDIT

I solve the issue.

the right code is:

<?php
        global $wpdb;
        $where = 'WHERE comment_approved = 1 AND user_id <> 0';
        $comment_counts = (array) $wpdb->get_results("
        SELECT user_id, COUNT( * ) AS total
        FROM {$wpdb->comments}
        {$where}
        GROUP BY user_id
        ORDER BY total DESC
    ", object);

            echo '<ul>';

            foreach ( $comment_counts as $count ) {
              $user = get_userdata($count->user_id);
            //echo "<pre>"; print_r($count); echo "</pre>";
            //echo "<pre>"; print_r($user); echo "</pre>";
              echo "<li>";
              echo get_avatar( $user->ID ) . "<a href='" . get_author_posts_url($user->ID) . "'>" . $user->display_name . "</a>";
              echo "<p> Numero di commenti: " . $count->total . "</p>" ;
              echo "</li>"

            ;
            }
            echo '</ul>';


    ?>

with the help of a WP Support Member I filter the comment counts by this:

ORDER BY total DESC

Thank you

4
  • Link to the source of the code please as it's not your work. You found it on the web, you mean someones website? Jul 21, 2015 at 15:30
  • Sorry @BradDalton. I found on the Wordpress Support: wordpress.org/support/topic/…
    – Elena
    Jul 21, 2015 at 15:33
  • Try WP_Query'orderby' => 'comment_count'. Not tested but an idea. Jul 21, 2015 at 15:38
  • unfortunately doesn't work :(
    – Elena
    Jul 21, 2015 at 16:06

0

Your Answer

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

Browse other questions tagged or ask your own question.