The Easy Way To Get Recent Comments In WordPress

I see lots of tutorials about how to get the most recent comments in WordPress. Many of these involve custom database queries and sometimes a few other additional contortions. There’s no need for any of that, WordPress provides a simple function that does all of the work for you – get_comments. It provides you with an array of comment objects, allowing you to massage the comment data into what ever format you want.

For those that don’t mind reading through the WordPress source code you can find the get_comments function in wp-includes/comment.php. This function first showed up in the 2.7 release, back at the end of 2008.

Here’s a simple example that will return the 5 most recently approved comments:

[sourcecode lang=”php”]
$recent_comments = get_comments( array(
‘number’ => 5,
‘status’ => ‘approve’
) );

That’s all there is to it. You can use get_comments to fetch comments based on a number options: author_email, ID, karma, number, offset, orderby, order, parent, post_id, status, type, and user_id. The power and flexibility of this function really shines through when you combine these options to get just the specific comments you are looking for.

Simplicity isn’t the only reason you should use get_comments instead of a home grown direct database query, it uses the core WordPress object cache hooks. If you are using an object cache plugin (memcached for instance) then this will reduce the number of queries that are sent to your database. Power, flexibility, and performance; what more could you ask for! ๐Ÿ™‚


  1. Was thinking the same thing while back when it was first introduced. Now that you mention it, it reminds me back of this:

  2. It would be a nice addition to be abel to filter on post_status and post_type.

  3. No response with the ticket yet? Bad patch? Hehe…

    If this goes through, the code for default Recent Comments widget can also be updated.

  4. Agreed, I was just looking at the recent comments widget to see if it could use get_comments().

  5. There is a far better solution to display the most recent comments of your blog: Wizzart – Customizable Recent Comments

    With this plugin you can customize the output of your comments for each widget directly in your backend configuration. Give it a try it will change the way you display recent comments ;-D

    kind regards

  6. There are several ‘recent comments’ widgets out there, I wasn’t ready to try and review them all ๐Ÿ™‚

  7. Sorry for a complete n00b question, but how would I modify this to make the most recent comment for a given post show up on the home page loop? Thanks!

  8. get_comments() also takes a post_ID argument, so you can pull the most recent comments for a specific post.

  9. Hi, Joseph I’m trying to use the code in my wordpress (2.9.2) sidebar in the following way, and it isn’t working out. Could you please suggest me, where I’m wrong?

    'status' => 'approve'
    ) );

  10. You may need to add debug code to find out what is happening.

  11. I stumbled onto this post, but it was great timing. My co-worker and I were just discussing how to do this earlier today.

    Is this the best option you’ve come across or do you prefer a widget?



  12. It is just code for fetching the data, putting in a widget or anything else is fine.

  13. How would you get the category of the comment?
    What I’m trying to do is display 5 recent comments from one particular category, and then display 5 recent comments from all the other categories

    I’ve tried a dozen plug ins that either don’t work or only load one instance. I’d rather just write and understand the code, if I can.
    I’m scouring the web for clues, but not finding what I need ๐Ÿ™

  14. Getting recent comments based on categories would be a little bit more work, since the categories are associated with posts not comments. Conceptually you’d need to get probably the 25 or so most recent comments and look up the post category for each, until you hit 5 for the category you want. If after going through all 25 you still haven’t found 5 for the category you want you’d need to fetch another 25 and repeat.

  15. So, I am using this code:
    $args = array(
    ‘status’ => ‘approve’,
    ‘number’ => ‘5’,
    $comments = get_comments($args);
    foreach($comments as $comm) :
    echo($comm->comment_author . ‘: ‘ . $comm->comment_content . ” );

    It works fine. However, do you know of a way that I can add a link to the actual comment?

  16. Hi, this is good, indeed, this is how the Recent Comments widget does it. However, I did notice a problem. If I have a private post and someone comments on it, immediately the comment shows up in Recent Comments (and by extension, anything using get_comments) along with the topic of the private post. Not so good.

    Are there any arguments I can add to “get_comments” so that it filters out Private post comments? Per this bug fix, the dashboard uses:

    if ( in_array( $comment->comment_approved, $allowed_states ) && current_user_can( ‘read_post’, $comment->comment_post_ID ) )

    Anyway we can do the same with get_comments?

  17. get_comments() should be improved so that it can filter out private posts.

  18. Would it be possible to only show the comments that occurred on a particular page?

  19. It might take a bit more work, but it is definitely possible.

  20. Found this through Google and thank you for clarifying that this can be done more efficiently. I’ve switched my previous code (which was a custom query) to this.

    The only issue that I’m having is that I want just a sample of the comment, like a preview of the comment in the footer of my website. Having long comments will break my layout.

    I’m using ‘echo $comment->comment_content’ to display the comment. Is there a ‘$comment->excerpt’ or something similar that can be used?


  21. And…I’ve answered my own question.

    Yes, there is a ‘comment_excerpt’ function directly built in and all you need is the comment ID, which is already brought in using the get_comments function.

  22. hi i want to show recent comment with fb avatar on home page

    facebook avatar display on my post page but not on home page recent comment

    please have a look the code of home page “recent comment”

    — This code display facebook avatar !

    i want to use below code with fb avatar ( it does not display fb avatar

    please reply me

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2019 Joseph Scott

Theme by Anders NorénUp ↑