Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Change the singel quotas to double like this AND comment_approved = "1" AND comment_type = "" See my answer below. –  Pontus Abrahamsson Jul 4 '12 at 7:49

1 Answer 1

Place this in your functions.php theme file:

<?php
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", "")
share|improve this answer
    
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. –  Boutros AbiChedid Jul 4 '12 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. –  Pontus Abrahamsson Jul 4 '12 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. –  Boutros AbiChedid Jul 4 '12 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? –  Pontus Abrahamsson Jul 4 '12 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. –  Boutros AbiChedid Jul 4 '12 at 20:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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