{"id":271,"date":"2011-06-28T23:11:33","date_gmt":"2011-06-28T23:11:33","guid":{"rendered":"http:\/\/www.codeboss.in\/web-funda\/?p=271"},"modified":"2011-06-28T23:11:33","modified_gmt":"2011-06-28T23:11:33","slug":"how-to-get-recent-comments-in-wordpress-explained-with-all-the-parameters","status":"publish","type":"post","link":"https:\/\/codeboss.in\/web-funda\/2011\/06\/28\/how-to-get-recent-comments-in-wordpress-explained-with-all-the-parameters\/","title":{"rendered":"How to get recent comments in WordPress explained with all the parameters"},"content":{"rendered":"<h3><u>Get all the values for all the comments<\/u><\/h3>\n<p>Suppose you want to show the recent comments for a WordPress blog. There is a ready made WordPress function for that.<br \/>\nIt is &#8211;<\/p>\n<p>get_comments()<\/p>\n<p>This function will return all the comments posted till date as an array of objects.<br \/>\nEach object contains the following fields-<\/p>\n<p>[comment_ID]<br \/>\n[comment_post_ID]<br \/>\n[comment_author]<br \/>\n[comment_author_email]<br \/>\n[comment_author_url]<br \/>\n[comment_author_IP]<br \/>\n[comment_date]<br \/>\n[comment_date_gmt]<br \/>\n[comment_content]<br \/>\n[comment_karma]<br \/>\n[comment_approved]<br \/>\n[comment_agent]<br \/>\n[comment_type]<br \/>\n[comment_parent]<br \/>\n[user_id]<\/p>\n<p>For example if you want all the Comments listed your code can be something as below &#8211;<\/p>\n<pre class=\"brush:php;\">\n$comments = get_comments();\nforeach($comments as $eachComment){\n     echo $eachComment->comment_content;\n     echo '<hr\/>';\n}\n<\/pre>\n<h3><u>Filter the output according to parameters<\/u><\/h3>\n<p>get_comments() accepts parameters as an array. Below are some example &#8211;<\/p>\n<h3><u>Show a number of latest comments<\/u><\/h3>\n<p>Latest 5 comments &#8211; get_comments(array(&#8220;number&#8221;=>5));<\/p>\n<h3><u>Show a number of comments starting from a certain position<\/u><\/h3>\n<p>Latest 5 comments from position 1 &#8211; get_comments(array(&#8220;number&#8221;=>5,&#8221;offset&#8221;=>1));<\/p>\n<p>The above condition will show 5 comments starting from the 2nd latest comment. The offset starts from value 0.<\/p>\n<h3><u>Show comments according to status<\/u><\/h3>\n<p>Approved &#8211; get_comments(array(&#8220;status&#8221;=>&#8221;approve&#8221;));<br \/>\nHold- get_comments(array(&#8220;status&#8221;=>&#8221;hold&#8221;));<br \/>\nSpam- get_comments(array(&#8220;status&#8221;=>&#8221;spam&#8221;));<br \/>\nTrash- get_comments(array(&#8220;status&#8221;=>&#8221;trash&#8221;));<\/p>\n<h3><u>Show comments according in a specific order<\/u><\/h3>\n<p>Ascending &#8211; get_comments(array(&#8220;order&#8221;=>&#8221;ASC&#8221;));<br \/>\nDescending &#8211; get_comments(array(&#8220;order&#8221;=>&#8221;DESC&#8221;));<\/p>\n<p>If only order parameter is there, it will order by &#8220;comment_date_gmt&#8221; field.<br \/>\nHowever if we want we can also include orderby parameter. Like &#8211;<\/p>\n<p>Order by comment_author ASC &#8211; get_comments(array(&#8220;orderby&#8221;=>&#8221;comment_author&#8221;,&#8221;order&#8221;=>&#8221;ASC&#8221;));<\/p>\n<p>Like this all the fields listed above that we get when we call get_comments() can be used as the value of &#8220;orderby&#8221; parameter.<\/p>\n<h3><u>Show the number of comments<\/u><\/h3>\n<p>get_comments(array(&#8220;count&#8221;=>&#8221;1&#8221;))<\/p>\n<p>The above code will return a value which is the number of comments posted.<br \/>\nAgain this can be used along with other parameters to know the count of certain type of comments. Like &#8211;<\/p>\n<p>Number of approved comments &#8211; get_comments(array(&#8220;count&#8221;=>&#8221;2&#8243;,&#8221;status&#8221;=>&#8221;approve&#8221;))<\/p>\n<p>Now play around with combination of various parameters and see what output you get \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8211; 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]&hellip; <a class=\"more-link\" href=\"https:\/\/codeboss.in\/web-funda\/2011\/06\/28\/how-to-get-recent-comments-in-wordpress-explained-with-all-the-parameters\/\">Continue reading <span class=\"screen-reader-text\">How to get recent comments in WordPress explained with all the parameters<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[35,65],"class_list":["post-271","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-comments","tag-wordpress","entry"],"_links":{"self":[{"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/posts\/271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/comments?post=271"}],"version-history":[{"count":0,"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/posts\/271\/revisions"}],"wp:attachment":[{"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/media?parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/categories?post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/tags?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}