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
- Use WordPress’s
get_comments()
function with thetype
argument to fetch comments, trackbacks, or pingbacks separately. - Create custom functions to return counts for each comment type.
- Add these functions to your theme’s
functions.php
file or a small custom plugin. - 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
- Open your active theme folder.
- Locate and open
functions.php
. - Paste the above functions at the end of the file.
- Save the file and upload if editing locally.
Option 2: Create a Small Custom Plugin
- Create a new folder named
comment-type-counts
inwp-content/plugins/
. - Create a file
comment-type-counts.php
inside that folder. - 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,
) );
}
?>
- Activate the plugin from the WordPress admin dashboard.
Step-by-Step Test
- Ensure your posts have comments, trackbacks, and pingbacks approved.
- Open your theme’s single post template file (usually
single.php
or a template part). - 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>