Exclude trackbacks from recent comments listing

To spare some time I tryed googleing for don’t show trackbacks in recent comments but wasn’t able to find anything relevant at first glance.

So, if you have the FreePress Recent Comments Widget and the long site titles from the trackbacks/pingbacks are breaking your design, here’s your quick tip from VileWorks:

  • go to Dasboard Plugins Recent Comments (edit)
  • find this line (line 234 in recentCommentsWidget.php):
    $request .= "AND comment_approved = '1' ORDER BY comment_ID DESC LIMIT $limit";
  • change it to:
    $request .= "AND comment_approved = '1' AND comment_type <> 'trackback' AND comment_type <> 'pingback' ORDER BY comment_ID DESC LIMIT $limit";

What you’re doing is adding a condition in the mySQL query that the comment type should not be trackback nor pingback (trackbacks and pingbacks are also stored in the wp_comments table in the database).

Good luck!

PS: I don’t know about other recent comments plugins but this particular one offers a pretty useful fp_get_recent_comments() function you can call in your theme. Here’s what parameters you can pass into it:

  • Number of comments to show: Total number of comments to display
  • Number of words per comment excerpt: The first N words of the comment will be listed
  • Max comments per post: This option is a “cap” to ensure that multiple posts are included in the list, even if all the recent comments were from a single post.
  • Max letters per word: This option is to deal with long “words” in the comments (specifically raw URLs) that can interfere with the formating of the list, depending on the CSS rules applied.

Update: Ray Fowler suggests in a comment below filtering out the admin comments

That way when I reply to a whole bunch of comments, I am not the only one showing up in the sidebar. Besides, I would rather highlight my guests rather than myself.

I did this with my recent comments widget by adding the extra condition and user_id !='1'. Where 1 is the admin’s id.

So the original line

$request .= "AND comment_approved = '1' ORDER BY comment_ID DESC LIMIT $limit";

would become

$request .= "AND comment_approved = '1' AND user_id  <> '1'  AND comment_type <> 'trackback' AND comment_type <> 'pingback' ORDER BY comment_ID DESC LIMIT $limit";

8 thoughts on “Exclude trackbacks from recent comments listing”

  1. Thanks, this is exactly what I was looking for. I am using WordPress 2.7 and the built in Recent Comments Widget. I was getting trackbacks and pingbacks in my sidebar, but I added your code to wp-includes/widgets.php and now they are filtered out perfectly.

    By the way, I also added this piece of code which filters out my own comments from the sidebar. That way when I reply to a whole bunch of comments, I am not the only one showing up in the sidebar. Besides, I would rather highlight my guests rather than myself. Here is the code:

    Changed:

    $wpdb->comments WHERE comment_approved='1'

    to:

    $wpdb->comments WHERE comment_approved='1' and user_id !='1'

    Thanks again!
    Ray

  2. @Ray Fowler: Yes… I just tested this out, mysql accepts both <> and !=.

    I used <> to keep it consistent with other queries they had in “FreePress Recent Comments Widget”… That’s what they were using.

Comments are closed.