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

wpcanyon.com

Get Separate Count For Comments, Trackbacks, And Pingbacks In WordPress

Posted on August 19, 2025 By Admin No Comments on Get Separate Count For Comments, Trackbacks, And Pingbacks In WordPress

Get Separate Count For Comments, Trackbacks, And Pingbacks In WordPress

If you want to display or process separate counts for comments, trackbacks, and pingbacks in WordPress, the default comment count won’t suffice. WordPress lumps all these types together, making it hard to distinguish between genuine user comments and automated or external references like trackbacks and pingbacks. This tutorial shows you how to get accurate, separate counts for each type quickly and reliably.

Quick Fix

  1. Use WordPress’s get_comments() function with the type argument to fetch comments, trackbacks, or pingbacks separately.
  2. Create custom functions to return counts for each comment type.
  3. Add these functions to your theme’s functions.php file or a small custom plugin.
  4. Use these functions in your templates or blocks to display separate counts.

Why This Happens

WordPress stores comments, trackbacks, and pingbacks in the same database table (wp_comments) and by default, functions like get_comments_number() or comments_number() return a combined count. Trackbacks and pingbacks are types of comments but serve different purposes:

  • Comments: User-generated feedback or discussion.
  • Trackbacks: Notifications from other blogs linking to your post.
  • Pingbacks: Automated notifications from other WordPress sites linking to your post.

Separating these counts helps you better understand engagement and manage spam or external references more effectively.

When to Use Separate Counts

  • Displaying genuine user interaction separately from automated or external references.
  • Filtering or moderating comments, trackbacks, and pingbacks differently.
  • Creating custom reports or analytics on engagement types.
  • Improving UI/UX by showing distinct counts in post meta or widgets.

Updated Code for Modern WordPress

Modern WordPress (5.0+) supports querying comments by type using the get_comments() function with the type parameter. Below are three functions to get counts for comments, trackbacks, and pingbacks separately.

/**
 * Get count of approved comments for a post.
 *
 * @param int $post_id Post ID.
 * @return int Number of approved comments.
 */
function get_approved_comment_count( $post_id ) {
    $comments = get_comments( array(
        'post_id' => $post_id,
        'status'  => 'approve',
        'type'    => 'comment',
        'count'   => true,
    ) );
    return $comments;
}

/**
 * Get count of approved trackbacks for a post.
 *
 * @param int $post_id Post ID.
 * @return int Number of approved trackbacks.
 */
function get_approved_trackback_count( $post_id ) {
    $trackbacks = get_comments( array(
        'post_id' => $post_id,
        'status'  => 'approve',
        'type'    => 'trackback',
        'count'   => true,
    ) );
    return $trackbacks;
}

/**
 * Get count of approved pingbacks for a post.
 *
 * @param int $post_id Post ID.
 * @return int Number of approved pingbacks.
 */
function get_approved_pingback_count( $post_id ) {
    $pingbacks = get_comments( array(
        'post_id' => $post_id,
        'status'  => 'approve',
        'type'    => 'pingback',
        'count'   => true,
    ) );
    return $pingbacks;
}

How to Add This Code

Option 1: Add to Your Theme’s functions.php

  1. Open your active theme folder.
  2. Locate and open functions.php.
  3. Paste the above functions at the end of the file.
  4. Save the file and upload if editing locally.

Option 2: Create a Small Custom Plugin

  1. Create a new folder named comment-type-counts in wp-content/plugins/.
  2. Create a file comment-type-counts.php inside that folder.
  3. Paste the following code into that file:
<?php
/*
Plugin Name: Comment Type Counts
Description: Provides functions to get separate counts for comments, trackbacks, and pingbacks.
Version: 1.0
Author: Your Name
License: GPL2
*/

function get_approved_comment_count( $post_id ) {
    return get_comments( array(
        'post_id' => $post_id,
        'status'  => 'approve',
        'type'    => 'comment',
        'count'   => true,
    ) );
}

function get_approved_trackback_count( $post_id ) {
    return get_comments( array(
        'post_id' => $post_id,
        'status'  => 'approve',
        'type'    => 'trackback',
        'count'   => true,
    ) );
}

function get_approved_pingback_count( $post_id ) {
    return get_comments( array(
        'post_id' => $post_id,
        'status'  => 'approve',
        'type'    => 'pingback',
        'count'   => true,
    ) );
}
?>
  1. Activate the plugin from the WordPress admin dashboard.

Step-by-Step Test

  1. Ensure your posts have comments, trackbacks, and pingbacks approved.
  2. Open your theme’s single post template file (usually single.php or a template part).
  3. Insert the following code snippet where you want to display the counts:
<?php
$post_id = get_the_ID();

$comment_count = get_approved_comment_count( $post_id );
$trackback_count = get_approved_trackback_count( $post_id );
$pingback_count = get_approved_pingback_count( $post_id );
?>

<div class="comment-type-counts">
    <p>Comments: <?php echo esc_html( $comment_count ); ?></p>
    <p>Trackbacks: <?php echo esc_html( $trackback_count ); ?></p>
    <p>Pingbacks: <?php echo esc_html( $pingback_count ); ?></p>
</div>
WordPress Snippets Tags:Comments, PHP, Pingbacks, Trackbacks, WordPress

Post navigation

Previous Post: Adding A Custom Field Automatically On Post/Page Publish
Next Post: Adding Classes To previous_posts_link() And next_posts_link() 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