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.
-
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); ?>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?i’ll do a comparison, if the comment author is not the post author:
http://codex.ww.wp.xz.cn/Database_Description#Table:_wp_usersif( $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); ?>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 countAlso, if there aren’t comments I get a nice “Warning: array_unique() [function.array-unique]: The argument should be an array in” š
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); ?>Understood, thanks.
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?my bad – i forgot to initialize the
$comm_authorsarray:<?php $comms = get_comments('post_id='.$post->ID); $comm_authors = array(); /initialize empty array foreach( $comms as $comm ) { etc...... ?>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 usersthat 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_idexist)<?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); ?>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 usersThanks.
1)
possibly a conflict with trashed comments, because is haven’t set this parameter inget_comments():$status
(string) (optional) Only return comments with this status.
‘hold’ – unapproved comments
‘approve’ – approved comments
‘spam’ – spam comments
‘trash’ – trash comments
Default: Nonechange 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.
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’?afaik, negation
status!=spamdoes 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');
The topic ‘Commenters per post’ is closed to new replies.