Your code is failing because the first term is the parent term, caused by checking both the parent and the child term.
You dont need to do that, if I have a heirarchy A->B->C->post, and I add the post to the C category, it’s automatically a part of its parents
Related Posts:
- Category tree is flattened inside admin upon saving
- Add child category to parent on post creation and save
- How to load several hierarchical categories from functions.php
- Most efficient use of custom taxonomies, categories, and pages
- Can’t custom taxonomies have same terms (slugs) as categories?
- How to display non-hierarchical taxonomy as terms with checkboxes?
- get_categories for custom post type with a specific custom taxonomy attached
- Order get_terms() By Custom Field
- How can I add the featured image functionality to a custom taxonomy?
- Combine two taxonomies in a hierarchical tree
- Different template for subcategories
- Use tag interface for hierarchical taxonomy?
- How can I select a primary category?
- Using URL parameters, list posts from category and custom taxonomy
- How to make custom taxonomy check boxes like ‘Categories’
- Different templates for parent and children categories/taxonomies
- Categories’ hierarchy in URL
- cat_is_ancestor_of() for custom taxonomies
- Are Categories, Tags and Custom Taxonomies any different in regards to SEO?
- How to create non-unique sub-category slugs?
- Show Post from parent category (custom taxonomy) ONLY!
- How to convert regular categories to custom taxonomy?
- How to only show posts on last child category
- How to get next previous category in same taxonomy?
- Removed slug from CPT, now How/where do I hook the filter to the taxonomy term archive pages link?
- Getting WordPress custom taxonomy/category?
- Are Custom Taxonomy Templates Possible?
- Display empty taxonomy terms with get_terms()
- Categories lose hierarchy order once assigned to post
- Get a list of Terms for a specific category
- Order Taxonomy Term by date created
- How to display a description of a given category from a custom taxonomy
- How to get the number of child categories a specific parent category has?
- How can I convert some categories to a custom taxonomy?
- Add Product categories to WordPress menu without losing hierarchy
- How can I get an tag to wrap each ancestor that gets outputted in this condition?
- how to get the categories for a single post in a hierarchical way
- Get multidimensional array that reflects category hierarchy
- PHP Fatal error: Cannot use object of type WP_REST_Response as array in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
- If statement within PHP echo
- How to know if it’s a child taxonomy?
- Taxonomies for WordPress Media Library [duplicate]
- wp_dropdown_categories and custom taxonomy + custom post type
- Categories vs. Page Site Organization
- Specifying multiple categories in URL (permalink)?
- Should I use custom taxonomy or custom post type
- How to enable category hierarchy for links?
- Should the actual /category/ directory be 404? Is that normal WP behaviour
- Limit number of terms that a custom taxonomy can save per custom post type
- Can you pre-check wordpress categories?
- Change existing taxonomy settings when using custom taxonomy
- How to get the id from the custom category?
- Combining post categories and link categories?
- One Specific Category To Permalink
- How to categorize posts under Category 1 and Category 2 which are separate lists of categories, and display them as separate lists in widgets
- add_rewrite_endpoint returns 404 on archives
- How to organize custom categories for filtering and selecting
- Custom sub taxonomy order by
- Add category only if post has custom taxonomy category
- Give a page priority over a category that has the same name?
- Categories overview that links to page displaying posts
- How can I edit the ‘Most Used’ tab in the categories meta-box to show another custom taxonomy?
- Get all media categories
- Get Number of total Parent Categories for Custom Taxonomy
- How do I make the category sidebar widget (stock) display the total # of posts including those in subcategories?
- Get posts from specific taxonomy term
- Display custom taxonomy attached to the post on post single page
- How to get Next/previous_post_link to go through articles in top parent category
- Why custom taxomony not showing when create a post?
- Limit categories and it’s subcategories for specific group of users
- Ive got a term (get_term_by) but now I want to filter it by a category it is in?
- Question on using custom structures for categories
- add slug beside name in Admin Category Checklists
- Hierarchical permalink structure for posts showing categories and sub-categories
- Target a specific category from a custom taxonomy?
- Get Categories Where Taxonomy Equals ‘n’
- WordPress list categories dropdown with parent-child relation and child under it’s parent
- Having a template per category? Bad for server overhead?
- Echo text using is_tag
- custom page for custom Taxonomy
- Custom page category taxonomy 404
- Filter By Category Dropdown List With Custom Taxonomies
- query_posts by category_name and custom taxonomy
- Display only subcategories of a category and not sub-subcategories
- Is it possible to create an alias/custom taxonomy for a category name?
- Same menu for different taxonomies to reach different content
- Showing HTML if Post is In Certain Taxonomy Term
- List child categories from parent category on custom taxonomy page
- I need to exclude from a query a category and a few custom taxonomies
- Category link redirect to custom template page instead of index.php?
- How could I write a get_categories_by_year() function?
- Assign a custom post to a custom taxonomy based on custom field value
- Check is category parent with ids from the childs – get_term_children
- Post to inherit custom category background image from parent
- Custom Fields Value As Taxonomy
- list all child categories that apply to current post even if those cats are empty?
- How to get posts using category slug in ClassiPress?
- 3.1 post formats and specific categories/custom taxonomies
- Link posts together (relative, not grouped)
- How to fix select filter on category