In WordPress 3.4.1: I am trying to add the Comment Author's total number of comments beside their info. (name + date/time) in the comment's list.

Here is the SQL that I am using:

$count = $wpdb->get_var('SELECT COUNT(comment_ID) FROM '. $wpdb->comments. ' WHERE comment_author_email = "' . get_comment_author_email() .'" ' AND comment_approved = '1' AND comment_type = '');

Basically The Author's email will be checked and the total number of comments will be displayed. But also I want only approved comments to be counted and also I don't want pingbacks/trackbacks to be counted. The above code is returning a syntax error and probably is wrong in terms of logic. Any help is appreciated. thanks.

  • Change the singel quotas to double like this AND comment_approved = "1" AND comment_type = "" See my answer below. Jul 4, 2012 at 7:49

2 Answers 2


Place this in your functions.php theme file:

function ps_count_user_comments() {
    global $wpdb;
    $count = $wpdb->get_var(
    'SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' 
    WHERE comment_author_email = "' . get_comment_author_email() . '" 
    AND comment_approved = "1" 
    AND comment_type IN ("comment", "")'

    return $count . ' comments';

This code will count the author comments and do NOT include Trackbacks/Pingbacks.

Then You print it like this:

<?php echo ps_count_user_comments(); ?>

You can try to run this in your SQL and change mail@example.com to your mail. I assume your database prefix is wp_ but if its not, just change wp_comments to your prefix.

SELECT COUNT(comment_ID) FROM wp_comments 
WHERE comment_author_email = "mail@example.com"  
AND comment_approved = "1" 
AND comment_type IN ("comment", "")
  • Thanks Pontus. That's great. At least I was on the right track. One problem though: Trackbacks/pingbacks are still counted (all have same constant number), it seems that comment_type = "" does not work even when I add 'comment' inside the quotes. How would I discriminate pingbacks/trackbacks from being counted? See image result with pingback counted as 13 (all pingbacks are some number): bacsoftwareconsulting.com/blog/wp-content/uploads/2012/07/… Thanks for your help. Jul 4, 2012 at 11:57
  • See my updated code, i have added comment_type not in ("trackback","pingback") so the SQL will look for all the comments that are Not in either trackback or pingback. Hope it works for you. Jul 4, 2012 at 12:27
  • Thanks Pontus. Even though logically what you added in the code make sense. It still does not respond, I am still getting trackbacks/pingbacks counted (same as the image). Any other ideas? Probably I should enclose the code in an if statement to exclude trackbacks/pingbacks. Jul 4, 2012 at 14:24
  • Okey, Can you try my updated code now? And if that not working can you update your question with some code where you adding the function? Jul 4, 2012 at 14:49
  • It still counts pingbacks/trackbacks. I even tried to use the check_comment() function [codex.wordpress.org/Function_Reference/check_comment] with no success. I think we should explore this function more in an if statement. I put the code in functions.php file and I edited the wp_list_comments() callback in functions.php so that it displays the comment count per author. I Could not add the code due to max allowed characters here. But The code is similar to what you have at the top. I tried all combinations of comment_type values. Thanks again. Jul 4, 2012 at 20:13

You can use the built-in WordPress comment query for that. I also use get_comments(); for this feature on my template.

For the parameters, I simply use 3 parameters, the same as you want.

    $comail = get_comment_author_email();
    $args = array(
                'author_email' => $comail,
                'status'  => 'approve',
                'type'  => 'comment',

wp_comment_query is limitless. So anything is possible. Multiple comment-type is also supported with the 'type__in' parameter.

Next, you can echo the count($comments);. You can also use 'count'=>'true', parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.