WordPress.org

Forums

Commenters per post (17 posts)

  1. carblanco
    Member
    Posted 3 years ago #

    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.

  2. 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

  3. carblanco
    Member
    Posted 3 years ago #

    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?

  4. i'll do a comparison, if the comment author is not the post author:
    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);
    ?>
  5. carblanco
    Member
    Posted 3 years ago #

    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

  6. carblanco
    Member
    Posted 3 years ago #

    Also, if there aren't comments I get a nice "Warning: array_unique() [function.array-unique]: The argument should be an array in" ;-)

  7. 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);
    ?>
  8. carblanco
    Member
    Posted 3 years ago #

    Understood, thanks.

  9. carblanco
    Member
    Posted 3 years ago #

    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?

  10. 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......
    ?>
  11. carblanco
    Member
    Posted 3 years ago #

    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

  12. 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);
    ?>
  13. carblanco
    Member
    Posted 3 years ago #

    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.

  14. 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.

  15. carblanco
    Member
    Posted 3 years ago #

    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'?

  16. 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');

  17. carblanco
    Member
    Posted 3 years ago #

    Finally working.
    Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags