You can use get_ancestors() to get parents and grandparents etc. of a term, then use that in wp_set_object_terms():
$term_id = 12;
// Get array of term parents.
$terms = get_ancestors( $term_id, 'topic' );
// Include original term in array.
$terms[] = $term_id;
// Add parents and original term to post.
wp_set_object_terms( $post_id, $terms, 'topic', true );
Related Posts:
- How to add images to taxonomies?
- Can you add the visual editor to the description field for custom taxonomies?
- Check if a post has any term in this custom taxonomy?
- Custom taxonomy hide meta box but show in menu
- display posts with same taxonomy term
- Is there a way to disable a term rather than deleting it?
- Modify Term Update Redirection
- Check if current term is a child of another term
- get_terms() doesn’t return empty terms even though hide_empty is false
- SQL QUERY needed to get POST category (taxonomy) ? – MUST be SQL statement
- Remove Category description textarea
- Archive template for taxonomy terms
- How can I set up a category “overview” page?
- Sort posts by number of matched terms
- Looping Through Custom Tax Terms and Displaying All Posts For Each
- Possible to register multiple custom taxonomies in one function?
- How to get the top most term (top ancestor) of a custom taxonomy child term?
- Hierarchical display of custom taxonomy
- Advanced Tax Query
- Function to limit the number of posts in taxonomy.php
- User role permissions based on taxonomies
- WP Query with custom taxonomy
- How to prevent posts duplicating when viewing a custom taxonomy term
- How to display custom taxonomy in multiple columns?
- get_the_terms() to show all custom taxonomies
- Custom taxonomies making WP very slow – Way to fix?
- Removing the “Popular Terms” area from the Taxonomy Edit Screen in the Admin Area
- Sorting taxonomy columns by meta value numeric
- Programatically added attribute, set to ‘show on product page’ automatically. Woocommerce [closed]
- Get Taxonmy Term ID For Current Post
- Is it possible to create exclusive custom taxonomy?
- ACF vs Taxonomies
- Reverse traversing taxonomy based on term_id
- Get Current Custom Taxonomy ID by Post ID
- Changing stylesheet depending on custom taxonomy terms
- Taxonomy with_front causes all
- Taxonomy archive showing no results
- I used the Tax-meta-class by bainternet but i still can’t get it to display any of the results.
- Autogenerate Front End Form Field With jQuery
- Changing taxonomy term by slug (wp_update_term)
- Rewrite Rule Working for all but one of the taxonomies created
- Taxonomies on custom taxonomies
- How to join result of different taxonomies?
- Extend & Search Native WordPress Image Galleries With Tags and Taxonomies?
- How to add Post Format Taxonomy Archive page in the menu?
- How to Rewrite Taxonomy URL to Include the Post Type as the Second Segment of the URL?
- How to get all taxonomies which can be added in menu?
- WordPress Tag or Custom Taxonomy Return All Posts if has that Word in Post Title
- woocommerce custom product category template
- Replace taxonomy permalinks
- Custom Taxonomy Term Archive Page Template that Filters 2 or more Taxonomies
- How to get posts which must match both custom taxonomies?
- Get posts in certain Taxonomy terms for the current page url
- How to add images to taxonomy terms? [duplicate]
- Why get_page_template() doesn’t show taxonomy template file name?
- Edit custom taxonomy description programatically
- Get taxonomy name for the current post
- View the complete list of a taxonomy in the navigation menu
- Get name of taxonomies of current page
- How to get attached image to taxonomy..?
- How to get list of only custom taxonomies?
- Add custom taxonomy as a css class
- Taxonomy page shows category in title
- Query custom taxonomy for category including children
- Only display latest custom taxonomy post
- How can I get Taxonomy Images to work with ‘orderby’ argument?
- Unsetting post_tag taxonomy breaks term description for other taxonomies
- Taxonomies on Pages with Custom Formatting
- How to combine taxonomies into URL?
- Amend taxonomy to search in wordpress admin
- Unique taxonomy for post
- Prioritise Pages over Taxonomy Term Root Archive, but not Taxonomy Term Child Archives
- Add terms to a taxonomy archive from within the same taxonomy
- Show recent posts from a custom taxonomy in wordpress
- Fill New Taxonomies
- add pagination in wordpress page template
- how do you pull data from two taxonomies?
- problem : Custom taxonomy template not showing
- Custom dropdown list taxonomy not saved
- How to get all the terms from a custom hierarchical taxonomy via REST api?
- Filter “get_terms” query
- Get post meta value outside of the loop
- Unable to create a new taxonomy
- Change link of taxonomy when get_the_term_list( is used?
- generate random slug when adding taxonomy
- Custom taxonomy shows full articles
- Change order custom taxonomy
- Register custom taxonomy from a Advenced custom field option page
- Using wp_query is it possible to orderby taxonomy?
- List of taxonomy archive index page links
- Custom Taxonomy Template Variables Available?
- Insert taxonomy term to different table
- wp_get_object_terms count on taxonomies within an category archive
- Make custom post type display with custom taxonomy in url
- Prevent random entries in hierarchical false taxonomies
- Exporting CSV of users with custom user taxonomies out of WordPress
- is_tax not working for custom taxonomy
- Add custom display condition to Elementor Theme Builder for custom taxonomy children, grandchildren, and great-grandchildren
- How to delete unused terms?
- Issue with WordPress Automatically Assigning Terms to Multiple Posts