{"id":1404,"date":"2020-02-05T21:07:03","date_gmt":"2020-02-05T21:07:03","guid":{"rendered":"https:\/\/xhynk.com\/content-mask\/?p=1404"},"modified":"2020-02-05T21:07:04","modified_gmt":"2020-02-05T21:07:04","slug":"stack-overflow-answer-60079535","status":"publish","type":"post","link":"https:\/\/xhynk.com\/content-mask\/wpse-answer\/stack-overflow-answer-60079535\/","title":{"rendered":"Stack Overflow Answer: 60079535"},"content":{"rendered":"\n<p>You could make use of WordPress&#8217; <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_term_children\/\"><code>get_term_children()<\/code><\/a> function on the <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/save_post\/\"><code>save_post<\/code><\/a> hook. A nice thing about this is that <code>get_term_children()<\/code> is recursive and collapses to a single dimensional array, so if you were to ever add smaller subdivisions as a third, fourth, fifth level, etc &#8211; you will automatically grab all hierarchical children with it.<\/p>\n\n\n\n<p>You don&#8217;t specifically mention what taxonomy you&#8217;re using, but you say <code>Category<\/code> in your post. I&#8217;ve used the <code>term\/taxonomy<\/code> functions instead of the <code>category<\/code> functions because it lets you adapt easier to custom terms\/taxonomies later a bit easier. In this scenario, ideally you&#8217;d have a custom taxonomy like &#8220;Location&#8221; and the terms &#8220;Africa, Asia, etc.&#8221; would be under that.<\/p>\n\n\n\n<p>This is a bit verbose and is a bit on the &#8220;verbose&#8221; side to be safe, but it should work to get you started at least:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( 'save_post', 'so_60079535_put_post_in_child_terms', 11 );\nfunction so_60079535_put_post_in_child_terms( $post_id ){\n    \/\/ Get the terms for this post\n    $taxonomy = 'category';\n    $terms    = wp_get_object_terms( $post_id, $taxonomy );\n\n    \/\/ If the tax doens't exist, or something fails, abort\n    if( is_wp_error($terms) )\n        return;\n\n    \/\/ If there are none in this tax, abort\n    if( empty($terms) )\n        return;\n\n    \/\/ Loop through terms to find any children, recursively\n    foreach( $terms as $term ){\n        $child_terms = get_term_children( $term->term_id, $taxonomy );\n\n        \/\/if tax doesn't exist or error, skip this term\n        if( is_wp_error($child_terms) )\n            continue;\n\n        \/\/ If no children, skip this term\n        if( empty($child_terms) )\n            continue;\n\n        \/\/ APPEND these child terms to the parent post object\n        wp_set_object_terms( $post_id, $child_terms, $taxonomy, true );\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You could make use of WordPress&#8217; get_term_children() function on the save_post hook. A nice thing about this is that get_term_children() is recursive and collapses to a single dimensional array, so if you were to ever add smaller subdivisions as a third, fourth, fifth level, etc &#8211; you will automatically grab all hierarchical children with it.&hellip; <a href=\"https:\/\/xhynk.com\/content-mask\/wpse-answer\/stack-overflow-answer-60079535\/\" class=\"more-link\">Read more <span class=\"screen-reader-text\">about Stack Overflow Answer: 60079535<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":"","_links_to":"","_links_to_target":""},"categories":[6],"tags":[],"test-tax":[],"class_list":{"0":"post-1404","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-wpse-answer","7":"entry","8":"has-post-thumbnail"},"_links":{"self":[{"href":"https:\/\/xhynk.com\/content-mask\/wp-json\/wp\/v2\/posts\/1404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xhynk.com\/content-mask\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xhynk.com\/content-mask\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xhynk.com\/content-mask\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xhynk.com\/content-mask\/wp-json\/wp\/v2\/comments?post=1404"}],"version-history":[{"count":1,"href":"https:\/\/xhynk.com\/content-mask\/wp-json\/wp\/v2\/posts\/1404\/revisions"}],"predecessor-version":[{"id":1405,"href":"https:\/\/xhynk.com\/content-mask\/wp-json\/wp\/v2\/posts\/1404\/revisions\/1405"}],"wp:attachment":[{"href":"https:\/\/xhynk.com\/content-mask\/wp-json\/wp\/v2\/media?parent=1404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xhynk.com\/content-mask\/wp-json\/wp\/v2\/categories?post=1404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xhynk.com\/content-mask\/wp-json\/wp\/v2\/tags?post=1404"},{"taxonomy":"test-tax","embeddable":true,"href":"https:\/\/xhynk.com\/content-mask\/wp-json\/wp\/v2\/test-tax?post=1404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}