Skip to content
  • Quick Ref
  • Contact
  • About
wpcanyon.com

wpcanyon.com

Showing Amount Of Comments A Comment Author Made

Posted on August 19, 2025 By Admin No Comments on Showing Amount Of Comments A Comment Author Made

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

  1. 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.
  2. Modify your comment template to display the count next to each comment author.
  3. 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

  1. Open your active theme’s functions.php file or create a new plugin file (e.g., comment-author-count.php).
  2. Paste the get_comment_author_comment_count() function into the file.
  3. Modify your comment template to call this function and output the count as shown above.
  4. 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

  1. Ensure your site has multiple comments from the same author (same email address).
  2. Add the function to functions.php or activate your plugin.
  3. Edit your theme’s comment template (usually comments.php) to call get_comment_author_comment_count( $comment ) and display the count.
  4. Visit a post with comments and verify the count appears next to the author’s name.
  5. 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 via render_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 or comment_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

FAQ

WordPress Snippets Tags:Comments, PHP, Users, WordPress

Post navigation

Previous Post: Adding Classes to the Current Page Item in wp_nav_menu()
Next Post: Showing Random Posts In WordPress

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Top WordPress Themes for Blogs in 2025
  • WordPress Admin Panel Trick: Adding ID Field to the Posts Listing
  • Solution previous_posts_link and next_posts_link Not Working
  • Show Top Commentators in WordPress Without a Plugin
  • How to Style Admin Comments in WordPress

Recent Comments

    Archives

    • August 2025

    Categories

    • Admin & Blocks
    • Admin & UI
    • Automation
    • Automation & Plugins
    • Comments
    • Comparisons
    • Database & Revisions
    • Developer Snippets
    • Fixes & Errors
    • Media & Thumbnails
    • Queries & Pagination
    • Security
    • Speed & Security
    • Tips & Tricks
    • WooCommerce How‑tos
    • WordPress Snippets
    • WordPress Themes
    • Terms & Conditions
    • Affiliate Disclosure

    Copyright © 2025 wpcanyon.com.

    Powered by PressBook WordPress theme

    Also by the maker of MySurveyReviews.com