Vile Works

Exclude Trackbacks from Recent Comments Listing

Hobby
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 Vile Works:

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

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

would become

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

Liked it? Subscribe to RSS or receive updates via email:


9 Comments Comments RSS Trackback URL

  1. Gravatar Icon Ray Fowler

    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

    Reply to this comment
  2. Gravatar Icon Stefan

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

    Reply to this comment
  3. Gravatar Icon Stefan

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

    Thanks.

    Reply to this comment
  4. Gravatar Icon Ray Fowler

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

    Reply to this comment
  5. Gravatar Icon Ray Fowler

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

    Reply to this comment
  6. Gravatar Icon Stefan

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

    Reply to this comment
  7. Gravatar Icon Ray Fowler

    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” (!=)?

    Reply to this comment
  8. Gravatar Icon Stefan

    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.

    Reply to this comment
  9. Gravatar Icon JamesD

    Thanks for the useful info. It’s so interesting

    Reply to this comment

Leave a Reply


info Vile Works uses Gravatars to display the avatars. You can get an account and have your avatar displayed on any site that supports it. We use the email address you provide when commenting to identify your avatar.

Security Code: