Remove Trackbacks and Pingbacks from Comments

Home/Remove Trackbacks and Pingbacks from Comments

Unfortunately, the default Recent Comments widget also includes trackbacks and pingbacks and the sight ain’t so lovely thus I had to add this hack to weed out the trackbacks and pingbacks.

Open your theme’s functions.php file and add the following code

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' );
}

Please note that the value red (DESC LIMIT 15) controls how many recent comments will appear in your sidebar and the order with which they will appear (ASC or DESC)

2011-03-02T23:20:07+00:00 June 7th, 2010|PHP Snippets|2 Comments

About the Author:

Warren Chandler is a freelance web programmer and web developer, based in Frinton on Sea, Essex. Warren specialises in web programming, web design, corporate identity, copy writing, SEO, logo and print media for home, work, and mobile platforms.

2 Comments

  1. Warren Chandler March 5, 2011 at 3:12 am - Reply

    This is Facebook me commenting!

  2. Clare Brown March 6, 2011 at 10:51 pm - Reply

    And me!

Leave A Comment