How to Exclude Pingbacks and Trackbacks From WordPress Comments Count

If you have enabled pingbacks and trackbacks on your WordPress website, it will also be considered while retrieving comments count for each of your post. For instance, if you have a post with 5 comments and 2 pingbacks, then the comment_number method will output 7 comments instead of 5. We can easily fix this issue by hooking into get_comments_number filter.

Quick Tip : How to Automatically Add a Class to the Attachment Images

To exclude pingbacks or trackbacks from WordPress comment counts copy the following code snippet and paste it inside your functions.php file located inside your theme directory.

function hide_trackbacks_pingbacks( $count ) {
	global $id;
	$comment_count = 0;
	$comments = get_approved_comments( $id );
	foreach ( $comments as $comment ) {
		if ( $comment->comment_type === '' ) {
			$comment_count++;
		}
	}
	return $comment_count;
}
add_filter( 'get_comments_number', 'hide_trackbacks_pingbacks', 0 );

 

 

1 Comments

Leave a Comment.