{"id":3779,"date":"2013-05-03T19:54:00","date_gmt":"2013-05-03T19:54:00","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=3779"},"modified":"2013-05-03T19:54:00","modified_gmt":"2013-05-03T19:54:00","slug":"getting-comments-count-in-wordpress","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/getting-comments-count-in-wordpress\/","title":{"rendered":"WordPress: Get Count For Approved Comments Outside The Loop"},"content":{"rendered":"<p>If you are a Web Developer and you work with WordPress, then there may have been a situation where you needed to get the count for \u00a0comments that were only in &#8220;Approved&#8221; state for a specific post. Note that this is different from the total comments count. So do you know how to <em><strong>get the comments count for Approved comments in WordPress<\/strong><\/em>? If not, then this article is for you. In this article, I am going to share a very easy way to get the count for all comments that are either Approved, Moderated, or in Spam and in Trash.<\/p>\n<p><!--more--><\/p>\n<h2>How to get comments count in WordPress for different comment states (Approved, Moderated, Spam, Trash ) outside WordPress loop<\/h2>\n<p>In order to get the comments count outside of the loop, we use the following function:<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;?php wp_count_comments( $post_id ); ?&gt;\n\npost_id (optional) - The ID of the post for which you want to get the comments count and it is optional inside the loop<\/pre>\n<p>In the above function, note that when you use it, you won&#8217;t directly get the comment count directly as wp_count_comments returns us an object. So rather you would have to point to the properties inside of the object to get it&#8217;s corresponding value. So let&#8217;s say we want to get the comments count for post with ID 10. I am going to list all possibilities below:<\/p>\n<pre class=\"lang:php decode:true\">&lt;?php \/\/10 is the post ID. Mention it explicitly outside the loop. Inside the loop, you would want to use get_the_ID()\n\n$comments_count = wp_count_comments( 10 );\n\n?&gt;<\/pre>\n<h3>\u00a0Get Approved Comments count:<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;?php echo \"Comments approved: \" . $comments_count-&gt;approved ; ?&gt;<\/pre>\n<h3>\u00a0Get Count of Comments that are in Moderation:<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;?php echo \"Comments in moderation: \" . $comments_count-&gt;moderated ; ?&gt;<\/pre>\n<h3>\u00a0Get Count of Comments that are in Spam:<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;?php echo \"Comments in Spam: \" . $comments_count-&gt;spam ; ?&gt;<\/pre>\n<h3>\u00a0Get Count of Comments that are in Trash:<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;?php echo \"Comments in Trash: \" . $comments_count-&gt;trash ; ?&gt;<\/pre>\n<h3>\u00a0Get the total count of comments, regardless of their state:<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;?php echo \"Total Comments: \" . $comments_count-&gt;total_comments ; ?&gt;<\/pre>\n<h5>That&#8217;s it!<\/h5>\n<p>Do you know of any other ways to <em><strong>get comments count in WordPress<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are a Web Developer and you work with WordPress, then there may have been a situation where you needed to get the count for \u00a0comments that were only in &#8220;Approved&#8221; state for a specific post. Note that this is different from the total comments count. So do you know how to get the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":114,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13,11],"tags":[31,30],"class_list":["post-3779","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips-and-tricks","category-wordpress-tips-and-tricks","tag-wordpress-snippets","tag-wordpress-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-YX","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=3779"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3779\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=3779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=3779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=3779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}