How to get recent comments in WordPress explained with all the parameters


Get all the values for all the comments

Suppose you want to show the recent comments for a WordPress blog. There is a ready made WordPress function for that.
It is –


This function will return all the comments posted till date as an array of objects.
Each object contains the following fields-


For example if you want all the Comments listed your code can be something as below –

$comments = get_comments();
foreach($comments as $eachComment){
     echo $eachComment->comment_content;
     echo '
'; }

Filter the output according to parameters

get_comments() accepts parameters as an array. Below are some example –

Show a number of latest comments

Latest 5 comments – get_comments(array(“number”=>5));

Show a number of comments starting from a certain position

Latest 5 comments from position 1 – get_comments(array(“number”=>5,”offset”=>1));

The above condition will show 5 comments starting from the 2nd latest comment. The offset starts from value 0.

Show comments according to status

Approved – get_comments(array(“status”=>”approve”));
Hold- get_comments(array(“status”=>”hold”));
Spam- get_comments(array(“status”=>”spam”));
Trash- get_comments(array(“status”=>”trash”));

Show comments according in a specific order

Ascending – get_comments(array(“order”=>”ASC”));
Descending – get_comments(array(“order”=>”DESC”));

If only order parameter is there, it will order by “comment_date_gmt” field.
However if we want we can also include orderby parameter. Like –

Order by comment_author ASC – get_comments(array(“orderby”=>”comment_author”,”order”=>”ASC”));

Like this all the fields listed above that we get when we call get_comments() can be used as the value of “orderby” parameter.

Show the number of comments


The above code will return a value which is the number of comments posted.
Again this can be used along with other parameters to know the count of certain type of comments. Like –

Number of approved comments – get_comments(array(“count”=>”2″,”status”=>”approve”))

Now play around with combination of various parameters and see what output you get 🙂

This entry was posted in Wordpress and tagged , . Bookmark the permalink.

One Response to How to get recent comments in WordPress explained with all the parameters

  1. Ram says:

    Thanks for the info. Just what I was looking for

Leave a Reply

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