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! 🙂

26 replies on “The Easy Way To Get Recent Comments In WordPress”

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

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

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

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'
) );

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?



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 🙁

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.

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?

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?

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?


Leave a Reply

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