{"id":2966,"date":"2014-06-01T10:12:43","date_gmt":"2014-06-01T09:12:43","guid":{"rendered":"http:\/\/www.simonbattersby.com\/blog\/?p=2966"},"modified":"2014-06-06T10:10:09","modified_gmt":"2014-06-06T09:10:09","slug":"getting-the-lowest-level-category-or-custom-taxonomy-for-a-wordpress-post","status":"publish","type":"post","link":"https:\/\/www.simonbattersby.com\/blog\/2014\/06\/getting-the-lowest-level-category-or-custom-taxonomy-for-a-wordpress-post\/","title":{"rendered":"Getting the lowest level category (or custom taxonomy) for a WordPress post"},"content":{"rendered":"<p>Working on a client project which applies a hierarchical custom taxonomy to a custom post type I needed to find the lowest level set for the post. Assuming that there is only one hierarchy applied to the post the following does the trick, for categories (running on the single post page):<\/p>\r\n<pre>$categories = get_categories();\r\n$parents = array();\r\nif($categories){\r\n\tforeach ($categories as $category){\r\n\t\t$ids[] = $category-&gt;term_id;<span class=\"code_comment\">\/\/read the category ids into an array\r\n<\/span>\t\tif ($category-&gt;parent) $parents[] = $category-&gt;parent;<span class=\"code_comment\">\/\/read the parent ids into an array\r\n<\/span>\t}\r\n}\t\t\r\n$low_categories =  array_diff ($ids,$parents);<span class=\"code_comment\">\/\/get the ids which are not parents\r\n<\/span>$lowest_category = array_shift($low_categories);<span class=\"code_comment\">\/\/read the first id (just in case there's more than one)\r\n<\/span><\/pre>\r\n<p>If you&#8217;re working with a custom taxonomy called, say, &#8216;classification&#8217; then this becomes:<\/p>\r\n<pre>$post_classifications = get_the_terms($get_the_ID,'classification');\r\nif($post_classifications){\r\n\tforeach ($post_classifications as $post_classification){\r\n\t\t$ids[] = $post_classification-&gt;term_id;\r\n\t\tif ($post_classification-&gt;parent) $parents[] = $post_classification-&gt;parent;\r\n\t}\r\n}\t\t\r\n$low_class =  array_diff ($ids,$parents);\r\n$classification = array_shift($low_class);<\/pre>\r\n<p>Hope this helps someone else&#8230;<\/p>\r\n\r\n","protected":false},"excerpt":{"rendered":"<p>Working on a client project which applies a hierarchical custom taxonomy to a custom post type I needed to find the lowest level set for the post. Assuming that there is only one hierarchy applied to the post the following does the trick, for categories (running on the single post page): $categories = get_categories(); $parents [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[10],"class_list":["post-2966","post","type-post","status-publish","format-standard","hentry","category-web-design-and-build","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/2966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/comments?post=2966"}],"version-history":[{"count":3,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/2966\/revisions"}],"predecessor-version":[{"id":2970,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/2966\/revisions\/2970"}],"wp:attachment":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/media?parent=2966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/categories?post=2966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/tags?post=2966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}