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";

Author of this post: Stefan Matei

Since 2007 Stefan Matei has been working as a freelance web designer and web developer. He describes his activity as "building WordPress websites that look good and feel right when you click or touch them." Interested in hiring Stefan for a project? Visit his portfolio at StefanMatei.com/portfolio
Sep 30, 2008     Posted under: archives