{"id":1516,"date":"2023-01-25T13:39:49","date_gmt":"2023-01-25T13:39:49","guid":{"rendered":"https:\/\/www.darrenlambert.com\/?p=1516"},"modified":"2023-01-25T13:45:57","modified_gmt":"2023-01-25T13:45:57","slug":"wordpress-how-to-get-the-post-count-for-a-term-and-post-type","status":"publish","type":"post","link":"https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/","title":{"rendered":"WordPress: How to get the post count for a term and post type"},"content":{"rendered":"

The WordPress function get_term<\/a> returns the count for the number of posts in the term, but this is for all post types.\u00a0 You need a custom function to count the number of posts in a term for a specific post_type.<\/p>\n

\/**\r\n* Returns the number of posts for a term in a taxonomy, for a post type\r\n* @param string $post_type\r\n* @param string $taxonomy\r\n* @return int count\r\n*\/\r\nfunction get_post_count_for_term_and_post_type( $term_id, $taxonomy, $post_type ) {\r\n\r\n  \/\/ Build the args\r\n  $args = array(\r\n    'post_type' => $post_type,\r\n    'posts_per_page' => -1,\r\n    'tax_query' => array(\r\n      array(\r\n        'taxonomy' => $taxonomy,\r\n        'field' => 'id',\r\n        'terms' => $term_id,\r\n      )\r\n    )\r\n  );\r\n\r\n  \/\/ Get the posts\r\n  $posts = get_posts( $args );\r\n\r\n  \/\/ Return the count\r\n  return count($posts);\r\n\r\n}<\/pre>\n

 <\/p>\n

 <\/p>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"

The WordPress function get_term returns the count for the number of posts in the term, but this is for all post types.\u00a0 You need a custom function to count the number of posts in a term for a specific post_type. \/** * Returns the number of posts for a term in a taxonomy, for a …<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10],"tags":[],"yoast_head":"\nWordPress: How to get the post count for a term and post type - Darren Lambert<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WordPress: How to get the post count for a term and post type - Darren Lambert\" \/>\n<meta property=\"og:description\" content=\"The WordPress function get_term returns the count for the number of posts in the term, but this is for all post types.\u00a0 You need a custom function to count the number of posts in a term for a specific post_type. \/** * Returns the number of posts for a term in a taxonomy, for a …\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/\" \/>\n<meta property=\"og:site_name\" content=\"Darren Lambert\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-25T13:39:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-25T13:45:57+00:00\" \/>\n<meta name=\"author\" content=\"Darren\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Darren\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/\",\"url\":\"https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/\",\"name\":\"WordPress: How to get the post count for a term and post type - Darren Lambert\",\"isPartOf\":{\"@id\":\"https:\/\/www.darrenlambert.com\/#website\"},\"datePublished\":\"2023-01-25T13:39:49+00:00\",\"dateModified\":\"2023-01-25T13:45:57+00:00\",\"author\":{\"@id\":\"https:\/\/www.darrenlambert.com\/#\/schema\/person\/ba3d82eb3f9925e946dae3b57e35bb79\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.darrenlambert.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"WordPress: How to get the post count for a term and post type\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.darrenlambert.com\/#website\",\"url\":\"https:\/\/www.darrenlambert.com\/\",\"name\":\"Darren Lambert\",\"description\":\"WordPress & WooCommerce Expert\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.darrenlambert.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.darrenlambert.com\/#\/schema\/person\/ba3d82eb3f9925e946dae3b57e35bb79\",\"name\":\"Darren\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.darrenlambert.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/076d975a0d00c5b2d53e2f76076cf5cf?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/076d975a0d00c5b2d53e2f76076cf5cf?s=96&d=mm&r=g\",\"caption\":\"Darren\"},\"url\":\"https:\/\/www.darrenlambert.com\/author\/darren\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"WordPress: How to get the post count for a term and post type - Darren Lambert","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/","og_locale":"en_US","og_type":"article","og_title":"WordPress: How to get the post count for a term and post type - Darren Lambert","og_description":"The WordPress function get_term returns the count for the number of posts in the term, but this is for all post types.\u00a0 You need a custom function to count the number of posts in a term for a specific post_type. \/** * Returns the number of posts for a term in a taxonomy, for a …","og_url":"https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/","og_site_name":"Darren Lambert","article_published_time":"2023-01-25T13:39:49+00:00","article_modified_time":"2023-01-25T13:45:57+00:00","author":"Darren","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Darren","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/","url":"https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/","name":"WordPress: How to get the post count for a term and post type - Darren Lambert","isPartOf":{"@id":"https:\/\/www.darrenlambert.com\/#website"},"datePublished":"2023-01-25T13:39:49+00:00","dateModified":"2023-01-25T13:45:57+00:00","author":{"@id":"https:\/\/www.darrenlambert.com\/#\/schema\/person\/ba3d82eb3f9925e946dae3b57e35bb79"},"breadcrumb":{"@id":"https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.darrenlambert.com\/wordpress-how-to-get-the-post-count-for-a-term-and-post-type\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.darrenlambert.com\/"},{"@type":"ListItem","position":2,"name":"WordPress: How to get the post count for a term and post type"}]},{"@type":"WebSite","@id":"https:\/\/www.darrenlambert.com\/#website","url":"https:\/\/www.darrenlambert.com\/","name":"Darren Lambert","description":"WordPress & WooCommerce Expert","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.darrenlambert.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.darrenlambert.com\/#\/schema\/person\/ba3d82eb3f9925e946dae3b57e35bb79","name":"Darren","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.darrenlambert.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/076d975a0d00c5b2d53e2f76076cf5cf?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/076d975a0d00c5b2d53e2f76076cf5cf?s=96&d=mm&r=g","caption":"Darren"},"url":"https:\/\/www.darrenlambert.com\/author\/darren\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.darrenlambert.com\/wp-json\/wp\/v2\/posts\/1516"}],"collection":[{"href":"https:\/\/www.darrenlambert.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.darrenlambert.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.darrenlambert.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.darrenlambert.com\/wp-json\/wp\/v2\/comments?post=1516"}],"version-history":[{"count":4,"href":"https:\/\/www.darrenlambert.com\/wp-json\/wp\/v2\/posts\/1516\/revisions"}],"predecessor-version":[{"id":1520,"href":"https:\/\/www.darrenlambert.com\/wp-json\/wp\/v2\/posts\/1516\/revisions\/1520"}],"wp:attachment":[{"href":"https:\/\/www.darrenlambert.com\/wp-json\/wp\/v2\/media?parent=1516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.darrenlambert.com\/wp-json\/wp\/v2\/categories?post=1516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.darrenlambert.com\/wp-json\/wp\/v2\/tags?post=1516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}