Title: Commenters per post
Last modified: August 20, 2016

---

# Commenters per post

 *  [carblanco](https://wordpress.org/support/users/carblanco/)
 * (@carblanco)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/)
 * Hi all,
 * Is there any way to show / count how many users have left a comment in a post?
   
   I mean, unique users, not total number of comments. For instance, if I left 2
   comments and you left 3 comments, I want to display that 2 users have commented.
 * Thanks.

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/commenters-per-post/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/commenters-per-post/page/2/?output_format=md)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136801)
 * possibly not the best example:
 *     ```
       <?php $comms = get_comments('post_id='.$post->ID);
       foreach( $comms as $comm ) { $comm_authors[] = $comm->comment_author; }
       $unique_comm_authors = array_unique($comm_authors);
       echo 'unique commenters: '.count($unique_comm_authors);
       ?>
       ```
   
 * [http://codex.wordpress.org/Function_Reference/get_comments](http://codex.wordpress.org/Function_Reference/get_comments)
 *  Thread Starter [carblanco](https://wordpress.org/support/users/carblanco/)
 * (@carblanco)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136802)
 * WOW, alchymyth, many thanks.
 * One more question…
    Now I want to count unique users who have participated in
   the post, I mean, unique commenters + 1 (post author). But what happen if the
   post author is also a commenter?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136804)
 * i’ll do a comparison, if the comment author is not the post author:
    [http://codex.wordpress.org/Database_Description#Table:_wp_users](http://codex.wordpress.org/Database_Description#Table:_wp_users)
 * `if( $comm->user_id != $post->post_author )`
 * this new code will not count the post author as commenter:
 *     ```
       <?php $comms = get_comments('post_id='.$post->ID);
       foreach( $comms as $comm ) {
       if( $comm->user_id != $post->post_author ) $comm_authors[] = $comm->comment_author; }
       $unique_comm_authors = array_unique($comm_authors);
       echo 'unique commenters: '.count($unique_comm_authors);
       ?>
       ```
   
 *  Thread Starter [carblanco](https://wordpress.org/support/users/carblanco/)
 * (@carblanco)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136805)
 * Thanks again.
 * I think that you are not counting the post author always and if it’s also a commenter
   then don’t count.
    I mean: – initially the post users are the author + commenters–
   if the author is also a commenter -> don’t count
 *  Thread Starter [carblanco](https://wordpress.org/support/users/carblanco/)
 * (@carblanco)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136809)
 * Also, if there aren’t comments I get a nice “Warning: array_unique() [function.
   array-unique]: The argument should be an array in” 😉
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136812)
 * > you are not counting the post author always and if it’s also a commenter then
   > don’t count.
 * i am not counting the post author because of your request:
    ‘Now I want to count
   unique users who have participated in the post, I mean, unique commenters + 1(
   post author). But what happen if the post author is also a commenter?’ – if this
   is not what you meant, please clarify.
 * > – initially the post users are the author + commenters
 * the code is not outputting post users, but post commenters which are not the 
   post author. i.e. the post author is never counted – neithre if he commented 
   on his own post, nor if has not commented.
 * if you want to show the number 1 if nobody has commented so far, or the only 
   commenter is the post author, the add 1 to the result of the code.
 * you can avoid the warning by adding a conditional statement
 *     ```
       <?php $comms = get_comments('post_id='.$post->ID);
       foreach( $comms as $comm ) {
       if( $comm->user_id != $post->post_author ) $comm_authors[] = $comm->comment_author; }
   
       if( $comm_authors$ ) : //avoid error warning if no comments
       unique_comm_authors = array_unique($comm_authors); endif;
       echo 'unique commenters: '.count($unique_comm_authors);
       ?>
       ```
   
 *  Thread Starter [carblanco](https://wordpress.org/support/users/carblanco/)
 * (@carblanco)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136814)
 * Understood, thanks.
 *  Thread Starter [carblanco](https://wordpress.org/support/users/carblanco/)
 * (@carblanco)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136815)
 * The only think I can’t understand is why the code works in the single.php but
   not in the home.php, posts with 0 comments appear with 5-6 commenters.
    What 
   am I not seeing here?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136817)
 * my bad – i forgot to initialize the `$comm_authors` array:
 *     ```
       <?php $comms = get_comments('post_id='.$post->ID);
   
       $comm_authors = array(); /initialize empty array
   
       foreach( $comms as $comm ) {
       etc......
       ?>
       ```
   
 *  Thread Starter [carblanco](https://wordpress.org/support/users/carblanco/)
 * (@carblanco)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136818)
 * Thanks.
 * Sorry for the confusion… what I need is counting how many users are joining a
   post.
    – If the post has 0 commentes, then we had 1 user (only the author) – 
   If the post has 3 comments made by other users, then we had 3 + 1 users – If 
   the post has 3 comments and 1 of them is made by the author, then we had 2 + 
   1 users
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136819)
 * that is what the code does – it counts external commenters – it does not count
   the post author, regardless if he has commented or not.
    (this assumes that the
   post author was logged in while commenting – only then will the `$comm->user_id`
   exist)
 *     ```
       <?php $comms = get_comments('post_id='.$post->ID);
       $comm_authors = array(); /initialize empty array
       foreach( $comms as $comm ) {
       if( $comm->user_id != $post->post_author ) $comm_authors[] = $comm->comment_author; } //conditional to avoid adding the post author
   
       if( $comm_authors$ ) : //avoid error warning if no comments
       unique_comm_authors = array_unique($comm_authors); endif;
   
       echo 'external commenters: '.count($unique_comm_authors);
       ?>
       ```
   
 *  Thread Starter [carblanco](https://wordpress.org/support/users/carblanco/)
 * (@carblanco)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136821)
 * Sorry for being a pain in the ass but:
 * 1. Something is not working properly in the home.php. In general is counting 
   right but for some reason there is a post with 0 comments and 4 external commenters…
 * 2. I think that the code is not counting admin comments. Is it possible?
    If 
   not, then always we have a -1 difference.
 * 3. Post with 0 comments are showing 0.
 * I mean:
    – If the post has 0 commentes, then we had 1 user (only the author) –
   If the post has 3 comments made by other users, then we had 3 + 1 users – If 
   the post has 3 comments and 1 of them is made by the author, then we had 2 + 
   1 users
 * Thanks.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136823)
 * 1)
    possibly a conflict with trashed comments, because is haven’t set this parameter
   in `get_comments()`:
 * > $status
   >  (string) (optional) Only return comments with this status. ‘hold’ –
   > unapproved comments ‘approve’ – approved comments ‘spam’ – spam comments ‘trash’–
   > trash comments Default: None
 * change the one line:
 *     ```
       <?php $comms = get_comments('post_id='.$post->ID.'&status=approve');
       ```
   
 * 2.
    as said before, the code is **not **counting admin (if the post is by admin)
   comments.
 * 3.
    ‘Post with 0 comments are showing 0.’ – that is the result of 2)
 * i said before, if you want to count the post author, you need to add 1 to the
   result of the code.
 *  Thread Starter [carblanco](https://wordpress.org/support/users/carblanco/)
 * (@carblanco)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136828)
 * 1) Same results…
    Using `<?php $comms = get_comments('post_id='.$post->ID.'&status
   =approve');` I get 4 external comments with 0 comments. Could we use ‘&status
   =approve&status!=trash&status!=spam&status!=hold’?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/#post-2136854)
 * afaik, negation `status!=spam` does not work.
 * another thought: the extra counts might be pings or trackbacks;
 * try and add:
    ‘type=comment’:
 * `<?php $comms = get_comments('post_id='.$post->ID.'&type=comment');`

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/commenters-per-post/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/commenters-per-post/page/2/?output_format=md)

The topic ‘Commenters per post’ is closed to new replies.

 * 16 replies
 * 2 participants
 * Last reply from: [carblanco](https://wordpress.org/support/users/carblanco/)
 * Last activity: [15 years, 1 month ago](https://wordpress.org/support/topic/commenters-per-post/page/2/#post-2137030)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
