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 –
get_comments()
This function will return all the comments posted till date as an array of objects.
Each object contains the following fields-
[comment_ID]
[comment_post_ID]
[comment_author]
[comment_author_email]
[comment_author_url]
[comment_author_IP]
[comment_date]
[comment_date_gmt]
[comment_content]
[comment_karma]
[comment_approved]
[comment_agent]
[comment_type]
[comment_parent]
[user_id]
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
get_comments(array(“count”=>”1”))
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 🙂
Thanks for the info. Just what I was looking for