Home | Articles | Wordpress | Filtering Comments in WordPress: Removing trackbacks and pingbacks from posts

Filtering Comments in WordPress: Removing trackbacks and pingbacks from posts

As WordPress becomes more popular in the Web Designer circles (is that possible?), many of the functions we are looking for are simply not integrated by default.  One of the functions I searched for for a long time was a way to filter comments so pingbacks and trackbacks wouldn’t be displayed on my posts or pages.  I personally don’t care much for a list of websites linking to my post or article being shown and inundating the discussion with useless information.

My personal preference is to do this through my functions.php file instead of using the dashboard to disable them, I still want them… I just don’t want them to show.

Trackback helps you to notify another author that you wrote something related to what he had written on his blog, even if you don’t have an explicit link to his article. This improves the chances of the other author sitting up and noticing that you gave him credit for something, or that you improved upon something he wrote, or something similar. With pingback and trackback, blogs are interconnected. Think of them as the equivalents of acknowledgements and references at the end of an academic paper, or a chapter in a textbook.

Pingback lets you notify the author of an article if you link to his article (article on a blog, of course). If the links you include in an article you write on a blog lead to a blog which is pingback-enabled, then the author of that blog gets a notification in the form of a pingback that you linked to his article.

The following code will filter trackbacks and pingbacks from your comment list and adjust the number of comments accordingly.

1. Edit your theme’s functions.php file
2. Paste the following code between the <?php and the ?>

// --------------------------------------------------------------------------
// Start Comment Filter
// --------------------------------------------------------------------------
	add_filter('comments_array', 'filterComments', 0);
	add_filter('the_posts', 'filterPostComments', 0);
	//Updates the comment number for posts with trackbacks
	function filterPostComments($posts) {
		foreach ($posts as $key => $p) {
			if ($p->comment_count <= 0) { return $posts; }
			$comments = get_approved_comments((int)$p->ID);
			$comments = array_filter($comments, "stripTrackback");
			$posts[$key]->comment_count = sizeof($comments);
		return $posts;
	//Updates the count for comments and trackbacks
	function filterComments($comms) {
	global $comments, $trackbacks;
		$comments = array_filter($comms,"stripTrackback");
		$trackbacks = array_filter($comms, "stripComment");
		return $comments;
	//Strips out trackbacks/pingbacks
	function stripTrackback($var) {
		if ($var->comment_type == 'trackback' || $var->comment_type == 'pingback') { return false; }
		return true;
	//Strips out comments
	function stripComment($var) {
		if ($var->comment_type != 'trackback' && $var->comment_type != 'pingback') { return false; }
		return true;

	if ( function_exists( 'add_theme_support' ) )
// --------------------------------------------------------------------------
// End Comment Filter
// --------------------------------------------------------------------------

Enjoy your filtered comments!

Note: These functions are not of my own creation, simply things I have found along the way.  If you are the author of this function, please let me know so I can properly credit you with your work!

About Joe D

I have always had a passion for everything computing. In early 2000, I decided to take my passion to the web. Thus, C.O.D. was born. Through the years we have made many great friends at C.O.D. and hope to continue our journey for years to come.

Check Also

Manage Multiple WordPress Sites with InfiniteWP

Running your website, or websites, on WordPress is an easy choice as the flexibility the …

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.