admin管理员组

文章数量:1126119

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?

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?

Share Improve this question asked Nov 16, 2014 at 3:38 jetlejjetlej 5721 gold badge6 silver badges24 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 );

本文标签: commentsWay to count the number of people who have commented on a post