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";
Posted in blog Tagged with: , ,
8 comments on “Exclude trackbacks from recent comments listing
  1. Ray Fowler says:

    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. Stefan says:

    @Ray Fowler: You’re welcome. For some reason your comment ended up in spam.

    And you’ve got an interesting point about not listing your own comments. I might use that myself.

  3. Stefan says:

    @Ray Fowler: And I did :) no more will my own comments show up in the footer.

    Thanks.

  4. Ray Fowler says:

    Very cool! I am glad I was able to help back.

  5. Ray Fowler says:

    Hmmm, I noticed in your code example, you have

    AND user_id  <> '1'

    instead of

    AND user_id !='1'

    I think that should be an = rather than an <>.

  6. Stefan says:

    @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.

  7. Ray Fowler says:

    I think I get it. So does mysql interpret <> as “less than or greater than a value” which works out to the same as “not equal” (!=)?

  8. Stefan says:

    Yeah, I think that’s what they thought when they made the SQL syntax…
    On the SQL page from the w3schools website “<>” is defined as the “not equal” operator.