{"id":266,"date":"2012-02-10T23:38:45","date_gmt":"2012-02-10T23:38:45","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=266"},"modified":"2012-02-10T23:38:45","modified_gmt":"2012-02-10T23:38:45","slug":"wordpress-total-post-count-of-a-category-and-its-child-categories","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/wordpress-total-post-count-of-a-category-and-its-child-categories\/","title":{"rendered":"WordPress: Total post count of a Category and its Child Categories"},"content":{"rendered":"<p>It may be desirable to show how may posts a Category contains in it &amp; also display the post count that the child categories of this (parent) category contains. So in <em><strong>WordPress to display total post count of a Category and its Child Categories<\/strong><\/em>, use the following little snippet of code.<!--more--><\/p>\n<h2>How to get total post count of a Category and its Child Categories in WordPress<\/h2>\n<p>Simply paste the following snippet in functions.php file of your WordPress theme:<\/p>\n<pre class=\"lang:php decode:true\">&lt;?php\n\nfunction get_post_count($id)\n{\n\n$count = 0;\n$taxonomy = 'category';\n$args = array(\n'child_of' =&gt; $id,\n);\n$tax_terms = get_terms($taxonomy,$args);\nforeach ($tax_terms as $tax_term) {\n$count +=$tax_term-&gt;count;\n}\nreturn $count;\n\n}\n\n?&gt;<\/pre>\n<p>Then simply paste the following code to display the post count of the Category that you desire &amp; it&#8217;s child categories. Simply paste the following code where you want the posts count to appear in your WordPress theme:<\/p>\n<pre class=\"lang:php decode:true\">&lt;?php\n\n\/\/ To get post count of category having category id 10.\n\/\/Change 10 to a number of your choice\necho get_post_count(10);\n\n?&gt;<\/pre>\n<h5>Your Turn!<\/h5>\n<p>Do you know of any other ways to <em><strong>get total post count of a Category and its Child Categories in WordPress<\/strong><\/em>? If yes, please feel free to share by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It may be desirable to show how may posts a Category contains in it &amp; also display the post count that the child categories of this (parent) category contains. So in WordPress to display total post count of a Category and its Child Categories, use the following little snippet of code.<\/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":[6,13,11,8],"tags":[31,30],"class_list":["post-266","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-snippets","category-tips-and-tricks","category-wordpress-tips-and-tricks","category-wordpress","tag-wordpress-snippets","tag-wordpress-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-4i","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/266","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=266"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/266\/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=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}