Exclude Trackbacks and Pingbacks from Defualt WordPress Recent Comments Widget

The default wordpress recent comments widget shows the most recent comments including the trachbacks and pingbacks too which can be some times annoying or useless in some cases.

So how do we remove/hide these trachbacks and pingbacks from wordpress default recent comments widget? as there is no option in the wordpress admin panel widget manager to set or select what to display in the recent comments widget box and what not to so then what to do now?

Well, the solution is pretty simple. We need to add some code to our functions.php file of the theme.

[This code edit is tested on WordPress (self hosted) version 2.9.2]

Goto

Appearance > Editor

Select the file

functions.php

At the end of the file functions.php add this code (just before the php closing tags ?> :

/* removing pingbacks from recent comments */
if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
$comments = $wpdb->get_results("SELECT $wpdb->comments.* FROM  $wpdb->comments JOIN $wpdb->posts ON $wpdb->posts.ID =  $wpdb->comments.comment_post_ID WHERE comment_approved = '1' AND  post_status = 'publish' AND comment_type IN ('comment', '') ORDER BY  comment_date_gmt DESC LIMIT 15");
wp_cache_add( 'recent_comments', $comments, 'widget' );
}

Now save the file. Delete the cache if you are using any cache plugin to see the changes made and then refresh your wordpress website/blog page and you will see that now the recent comments widget shows only the recent comments excluding any pingbacks and trackbacks.

hire best wordpress php css developer
Need Help? Hire me now!
I can help you in anything related to webdevelopment, including: HTML, CSS, PHP & MySql, WordPress (plugins & themes development, others), regular expressions, server management, etc.

10 comments on “Exclude Trackbacks and Pingbacks from Defualt WordPress Recent Comments Widget

  1. this is a good way of removing undesireable comments.. i was too looking for this type of feature for a long time ..thanks for sharing with us..

  2. this is a good way of removing undesireable comments.. i was too looking for this type of feature for a long time ..thanks for sharing with us..

  3. Just letting you know that I very carefully did exactly what you said and it totally crashed my site. The weird thing is that when I pasted the original functions.php code back it still wouldn’t go back up. Thanks to Hostgator tech support they were able to fix it for me but it just goes to show that this hack doesn’t work with all themes…and darn, I really wish it did.

Leave a Reply

Your email address will not be published.