I have deep hierarchical taxonomy and I want to have all parent term assigned when I select a child term. I need it for category structure on a online listing/classified site.
CPT name: product
Taxonomy Name: product_cat
I have deep hierarchical taxonomy and I want to have all parent term assigned when I select a child term. I need it for category structure on a online listing/classified site.
CPT name: product
Taxonomy Name: product_cat
Comments are closed.
Hooking into
save_post
action.While loop ensures we go upward until we hit top level terms.
The options above should work, this function will work for any object whenever terms are set, and using
set_object_terms
allows you to let the action handle traversing up the hierarchyThis gist will have any updates or patches if I add them later:
https://gist.github.com/tripflex/65dbffc4342cf7077e49d641462b46ad
The code has some bugs as provided above. Use the following code to also make in working in the quick edit modus: How to hook into the quick edit action? (provided by Pieter Goosen)
Improved! Thks @Sisir
Now, you can define more than one post type and term.
You can define Allowed Post Types in ($arrayPostTypeAllowed) array, and Allowed Terms in ($arrayTermsAllowed) array.