[wp-hackers] function to show total comments by user

Will Anderson will at itsananderson.com
Sun Jan 16 20:53:50 UTC 2011


Why not just use the comment author email instead? Author name isn't
necessarily unique.

function comment_author_comments_count($comment_author_email) {
    $author_comments =  get_comments( array( 'author_email' => $ ) );
    echo count( $author_comments );
}

// In comment template
comment_author_comments_count( comment_author_email() );

On the other hand, it might be better to simply build a custom SQL query to
get the count, rather than grabbing all the author's comments but not using
them.

function comment_author_comments_count($comment_author_email) {
    global $wpdb;
    echo $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) AS total
FROM {$wpdb->comments} WHERE comment_author_email = %s",
$comment_author_email );
}

The second method will be MUCH more space/time efficient than the first.


On Sun, Jan 16, 2011 at 3:36 PM, danilo <danixland at gmail.com> wrote:

> On 16/01/2011 21:21, Simon Blackbourn wrote:
>
>> Now my questions are, have I reinvented the wheel? While I wasn't able to
>>> find a function within WordPress that does exactly this, maybe you can
>>> point
>>> me to something I've missed in my research...
>>>
>>>
>>>
>> This should work (replace the 1 with your required user ID):
>>
>> $userID = 1;
>> echo get_comments( array( 'user_id' =>  $userID, 'count' =>  true ) );
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
>>  Thanks for replying,
> reading trough the codex for get_comments() there's not a single word about
> the 'count' option...
> I think your solution also doesn't show the total comments for unregistered
> users. I could use 'author_email' instead of 'user_id' but at this point I
> don't know how to retrieve the email associated with the commenter.
>
> Also trying to use your solution it returned an array, so I suppose the
> 'count' parameter doesn't work...
>
>
> --
> Danilo aka danix
> http://danixland.net
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list