Showing Amount Of Comments A Comment Author Made
When managing a WordPress site, you might want to display how many comments a particular comment author has made. This can add social proof, encourage engagement, or simply provide context to readers. The quick fix is to use a custom function that counts comments by author and then display that count alongside their comment.
Quick Fix: Count Comments by Author WordPress
- Add a custom function to your
functions.php
file or a small custom plugin that counts comments by the author’s email or user ID. - Modify your comment template to display the count next to each comment author.
- Test to ensure the count updates correctly for each commenter.
Why This Happens
By default, WordPress does not show how many comments a user or guest commenter has made. This is because comments are stored individually without aggregation by author. To display the number of comments an author has made, you need to query the database and count comments associated with that author’s email or user ID.
This is especially useful for community sites, forums, or blogs with active discussions where recognizing frequent commenters adds value.
Step-by-Step: Updated Code for Modern WordPress
The following example uses WordPress core functions and best practices to count comments by author email (works for both logged-in users and guest commenters):
<?php
/**
* Get the number of approved comments by a comment author.
*
* @param WP_Comment $comment Comment object.
* @return int Number of approved comments by the author.
*/
function get_comment_author_comment_count( $comment ) {
if ( ! $comment instanceof WP_Comment ) {
return 0;
}
$author_email = $comment->comment_author_email;
if ( empty( $author_email ) ) {
return 0;
}
$args = array(
'author_email' => $author_email,
'status' => 'approve',
'count' => true,
);
$count = get_comments( $args );
return (int) $count;
}
?>
To display this count next to each comment author in your theme’s comments.php
or comment template part, add:
<?php
$comment_count = get_comment_author_comment_count( $comment );
if ( $comment_count > 1 ) {
echo '<span class="comment-author-count">(' . esc_html( $comment_count ) . ' comments)</span>';
}
?>
How to Add via functions.php
or a Small Plugin
- Open your active theme’s
functions.php
file or create a new plugin file (e.g.,comment-author-count.php
). - Paste the
get_comment_author_comment_count()
function into the file. - Modify your comment template to call this function and output the count as shown above.
- Save and upload the changes.
If you prefer a plugin, wrap the function in plugin headers like this:
<?php
/*
Plugin Name: Comment Author Comment Count
Description: Displays the number of comments a comment author has made.
Version: 1.0
Author: Your Name
License: GPL2
*/
function get_comment_author_comment_count( $comment ) {
if ( ! $comment instanceof WP_Comment ) {
return 0;
}
$author_email = $comment->comment_author_email;
if ( empty( $author_email ) ) {
return 0;
}
$args = array(
'author_email' => $author_email,
'status' => 'approve',
'count' => true,
);
$count = get_comments( $args );
return (int) $count;
}
?>
Step-by-Step Test
- Ensure your site has multiple comments from the same author (same email address).
- Add the function to
functions.php
or activate your plugin. - Edit your theme’s comment template (usually
comments.php
) to callget_comment_author_comment_count( $comment )
and display the count. - Visit a post with comments and verify the count appears next to the author’s name.
- Try commenting again with the same email and refresh to see the count increase.
Block Themes & Gutenberg Notes
With the rise of block themes and Gutenberg, comments are often rendered via blocks or server-side rendering. To integrate this functionality:
- If your theme uses the
core/comment
block, you may need to create a custom block variation or extend the comment rendering viarender_callback
in PHP. - Use the same
get_comment_author_comment_count()
function inside your block’s PHP render callback to output the count. - For block-based themes, consider creating a small plugin that hooks into comment rendering filters like
comment_text
orcomment_author
to append the comment count dynamically.
Common Pitfalls
- Counting by email only: Guest commenters with different emails will be counted separately even if they are the same person.
- Performance: Counting comments for every comment on a large site can cause performance issues. Consider caching results if needed.
- Comment moderation: Only approved comments are counted to avoid showing counts for spam or pending comments.
- Theme compatibility: Some themes heavily customize comment output; ensure you insert the code in the correct template file.
Works on
Environment | Compatibility |
---|---|
Web Servers | Apache, Nginx, LiteSpeed |
Control Panels | cPanel, Plesk, DirectAdmin |
WordPress Versions | 5.0 and above (modern block and classic themes) |
Themes | Classic PHP templates, Block themes with minor adjustments |