0

There doesn't seem to be an easy WP way to show the total number of different commenters, just total # of comments.

Is there a better than manually loop through all comments and counting the unique user ID's?

1 Answer 1

2

You could try to count the number of unique comment author emails per post:

/**
 * Number of unique comment author emails per post:
 *
 * @see    http://wordpress.stackexchange.com/a/168606/26350
 * @param  int $pid
 * @return int
 */
function get_unique_commenters_by_post_id( $post_id )
{
   global $wpdb;
   $sql = "SELECT COUNT(1) as uc FROM ( 
              SELECT COUNT(1) as c FROM {$wpdb->comments} 
                  WHERE comment_post_ID = %d 
                  GROUP BY comment_author_email
           ) as t";
    return $wpdb->get_var( $wpdb->prepare( $sql, $post_id ) );
}

Usage example:

echo get_unique_commenters_by_post_id( $post_id = 213 );
4
  • How would I echo that in the single post loop so it catches the current post id?
    – Pete
    Feb 21, 2018 at 9:35
  • 1
    I wonder if you mean echo get_unique_commenters_by_post_id( get_the_ID() ); @Pete
    – birgire
    Feb 21, 2018 at 13:48
  • I found this worked... <?php echo get_unique_commenters_by_post_id( $post_id = $post->ID ); ?> in my loop.
    – Pete
    Feb 21, 2018 at 23:27
  • @Pete glad to hear it worked for you.
    – birgire
    Feb 22, 2018 at 8:22

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.