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.
Place this in your functions.php theme file:
This code will count the author comments and do NOT include Trackbacks/Pingbacks.
Then You print it like this:
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.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.
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.Bikran solutions can also uses as function, so you can use in several places at your theme. Also, I think he’s code is needing an extra line before
count($comments)
:So the ready-to-use function would be:
And uses: