{"id":77,"date":"2011-04-14T13:07:36","date_gmt":"2011-04-14T13:07:36","guid":{"rendered":"http:\/\/wordpress.agileadam.com\/?p=77"},"modified":"2014-01-22T04:03:05","modified_gmt":"2014-01-22T04:03:05","slug":"get-taxonomy-terms-by-count","status":"publish","type":"post","link":"https:\/\/agileadam.com\/2011\/04\/get-taxonomy-terms-by-count\/","title":{"rendered":"Get Taxonomy Terms by Count"},"content":{"rendered":"

This function returns an array of taxonomy term objects. It is much like taxonomy_get_tree()<\/em>, and it does in fact use this function. We had a client that wanted a grid of product categories with an image for each term, but only for terms that have at least X associated nodes. This problem was solved using the function below, as well as another custom function to alter the views query (I’ll post about this later).<\/p>\n

\/**\r\n * Returns an array of taxonomy terms that have been associated with at least X nodes\r\n *\r\n * @param $vid\r\n *   The vocabulary id\r\n * @param $min_items\r\n *   The minimum number of items that have been associated with this term\r\n * @return\r\n *   An array of taxonomy term objects\r\n *\/\r\nfunction mymodule_get_terms_by_count($vid, $min_items = -1){\r\n  $terms = taxonomy_get_tree($vid);\r\n  $items = array();\r\n  foreach ($terms as $term) {\r\n    $count = taxonomy_term_count_nodes($term->tid);\r\n    if ($count == -1 || $count >= $min_items) {\r\n      $items[] = $term;\r\n    }\r\n  }\r\n\r\n  return $items;\r\n}<\/pre>\n

I’d like to also mention that I toyed with the idea of adding a $max_items<\/em> variable. It’s simple enough to do, but I figured (maybe incorrectly) that you wouldn’t ever need to say, “Give me all terms that have between 10 and 30 associated records.”<\/p>\n","protected":false},"excerpt":{"rendered":"

This function returns an array of taxonomy term objects. It is much like taxonomy_get_tree(), and it does in fact use this function. We had a client that wanted a grid of product categories with an image for each term, but only for terms that have at least X associated nodes. This problem was solved using the function below, as well as another custom function to alter the views query (I’ll post about this later). \/** * Returns an array of taxonomy terms that have been associated with at least X nodes * * @param $vid * The vocabulary id * @param $min_items * The minimum number of items that have been associated with this term * @return * An array of taxonomy term objects *\/ function mymodule_get_terms_by_count($vid, $min_items = -1){ $terms = taxonomy_get_tree($vid); $items = array(); foreach ($terms as $term) { $count = taxonomy_term_count_nodes($term->tid); if ($count == -1 || $count >= $min_items) { $items[] = $term; } } return $items; } I’d like to also mention that I toyed with the idea of adding a $max_items variable. It’s simple enough to do, but I figured (maybe incorrectly) that you wouldn’t ever need to say, “Give me all terms that have between 10 and 30 associated records.”<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[79],"tags":[84,85,37,38],"_links":{"self":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/77"}],"collection":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":3,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":80,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/77\/revisions\/80"}],"wp:attachment":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}