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.
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..
you are welcome ovais :) i am glad that it was of some help
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..
You are welcome Amy. I am glad that it was helpful for you :)
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.
Can i see your website and the name of theme you are using?
Nice post…Thank you for sharing some good things!!
you’re welcome
thanks for the useful post.
Is there a way to exclude comments from a specific post in the recent comments widget?
you mean show widget on some posts and not on others? please explain abit