Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Saving Child Terms on front end not setting parent

To fix all the older posts that you already posted with only the child term.
You need run this function only 1 time and its will add you the parents terms. its for all the ancestors of the child term.

Make sure to backup the DB before you run this function.

function fix_terms_parents() {
    /**
     * Create a query that select all the posts in your post type
     */
    $query = new WP_Query(array('post_type' => 'property', 'posts_per_page' => -1));
    if ( $query->have_posts() ):
        while ( $query->have_posts() ) : $query->the_post();
            $post_id = get_the_ID();
            /**
             * Get all the terms that set to the post
             */
            $terms = get_the_terms($post_id, 'city-type');
            foreach($terms as $term) {
                /**
                 * Get the term ancestors and append them
                 */
                $ancestors = get_ancestors($term->term_id, 'city-type');
                if($ancestors) {
                    $new_terms = wp_set_object_terms($post_id, $ancestors, 'city-type', true);
                    // the $new_terms variable is array of all the new terms that you set
                }
            }
        endwhile;
        wp_reset_postdata();
    endif;
}

Here is a function to set all the term ancestors. you can use with the return that you get from wp_set_object_terms in your old code.

/**
 * @param int $post_id the post id
 * @param string $taxonomy
 * @param array terms ids
 *
 * @return array of the ancestors terms that was append
 */
function set_ancestors_terms($post_id, $taxonomy, $terms) {
    foreach($terms as $term_id) {
        $ancestors = get_ancestors($term_id, $taxonomy);
        if($ancestors) {
            // Append parent terms
            return wp_set_object_terms($post_id, $ancestors, $taxonomy, true);
        }
    }
}

The first function you need to run only 1 time put it in the function.php and add add_action( 'init', 'fix_terms_parents', 10); too before it to run it.
After this you can delete this function its only to fix the old posts.

Before you run the first function create DB backup.

The Second function you add too to the functions.php and you run it in your old code when you set the terms you need to get the return like this and run the function.

$terms_array = wp_set_object_terms($pid, $nhb_type_value, 'city-type');
set_ancestors_terms($pid, 'city-type', $terms_array);

Related Posts:

  1. Does WordPress limit the length of slug names for Post Meta or Terms?
  2. Advanced search form with filters for custom taxonomies and custom fields
  3. Get terms by taxonomy AND post_type
  4. Get Posts by Custom Post Type ,Taxonomy, and Term
  5. Get The Post Type A Taxonomy Is Attached To
  6. Display current taxonomy term when inside custom post type
  7. Get term slug of current post
  8. How to limit the number of terms (terms acts like categories)
  9. How do I list custom taxonomy terms without the links?
  10. query multiple taxonomies
  11. wp_set_object_terms() is not replacing term, but creating a new one
  12. Get terms that contain posts that in turn belong to other terms?
  13. wp_insert_term doesn’t work with custom post type’s taxonomy
  14. Custom Taxonomies Terms as Post Title for Custom Post Types upon Publishing
  15. wp_get_object_terms(): count relative to passed IDs?
  16. Retrieve single term slug
  17. Not Able to Insert Taxonomy Term Using wp_insert_post()
  18. array_pop() expects array, boolean given for $event_type_term ->slug
  19. How can I add programmatically custom taxonomy terms to a custom type post when saving posts?
  20. post count is wrong when using same taxonomy for 2 different CPT
  21. Glossary with Custom Post Type
  22. Custom Post Type Archive Template
  23. Maintaining strict one-to-one association between terms and custom posts
  24. Displaying custom post type by first letter through custom taxonomy
  25. Retrieve a specific field from taxonomy term through advanced custom fields [closed]
  26. If on term-page -> get the current term?
  27. Separate Custom Post Type Taxonomy by Comma
  28. Sort the main query in subcategories/terms?
  29. Reason action hook won’t work with update_post_meta from frontend form? Alternative?
  30. Trouble adding custom featured image for custom taxonomy TERMS
  31. front end radio custom taxonomy with custom post type
  32. Get child terms of current term of the current post
  33. Assign category to front end post
  34. Custom Meta Boxes – Nonce Issue – Move to trash issue
  35. Get list of terms of current taxonomy archive for another taxonomy
  36. Creating a function that receives the taxonomy terms that have been changed in a custom post type
  37. Get all taxonomies for all post types
  38. Get the terms of a custom taxonomy for a specific author in author template
  39. Dynamically create/remove terms in taxonomy when custom post type is published/trashed
  40. Show metabox in custom-post-template depending on taxonomy term?
  41. How to get the first term for the current taxonomy?
  42. How do I provide a “show all posts” link in a paginated term archive?
  43. Displaying Metabox value (custom post type taxonomy)
  44. Output the content of a term organised by each of the CPT?
  45. How to filter the taxonomy terms based on another taxonomy term
  46. How can I display my custom metaboxes on a custom post template?
  47. How to get posts from multiple custom post types according to it’s custom taxonomy terms?
  48. need advice on how to do a lists using custom post types – taxonomy vs postmeta
  49. Count posts with specific term_meta
  50. How to output wordpress custom tags separated by comma?
  51. How to check the terms in single custom post type template
  52. Displaying a custom post types custom taxonomy value?
  53. get_the_terms return only last term
  54. get_post_meta returns empty array for terms
  55. Show listings from Impress Listing plugin in random order using taxonomy and terms
  56. wp_set_object_terms() not adding new term to custom post and custom taxonomy
  57. Update post terms with custom taxonomy
  58. How do I display tags for a custom post type single page?
  59. Custom Post Type Archive Page Filtering
  60. Filtering WP_Query Dynamically on the Front-End
  61. Get template part using a custom taxonomy term
  62. Get posts by querying taxonomy and certain terms of the taxonomy?
  63. How to restrict users and admin from creating new taxonomy terms?
  64. How to save custom taxonomy from front end post submission
  65. using $wpdb to get custom post type with term
  66. Show posts from two or more custom taxonomy terms
  67. Allow user to set custom order to a list of custom taxonomies?
  68. Displaying custom taxonomy in the admin list of a custom post type
  69. Which post does a taxonomy term belongs to?
  70. Posting to a Custom Post Type from front end – user generated content
  71. Display post as term id
  72. How to duplicate entire custom post type
  73. How do I display the taxonomy term alongside the post type post title?
  74. Issue on Creating Custom Tax Term Dynamically From Another CPT Meta-box On Publish
  75. Post count by month of taxonmy term
  76. WordPress loop: Show only a Custom Post Type Taxononmy TERM
  77. Metabox not show in categories custom post type cmb2
  78. How do I display the taxonomy for a custom post type in an array
  79. Display Custom Taxonomy Alphabetically
  80. How do I require the specification of term in a custom post type and custom taxonomy?
  81. Custom Post, set object Taxonomy terms in plugin
  82. Get terms that contain posts that in turn belong to other terms?
  83. How to get custom posts sub category link
  84. Custom while loop for hierarchical display of a taxonomy
  85. Lists custom taxonomy terms that has specific custom field value assigned to the term (not post)
  86. Display taxonomy terms, child terms and posts in a template
  87. How to make front end form only accept certain values
  88. Use custom walker to add taxonomy terms to main nav menu
  89. Add term to custom post type on draft
  90. Displaying custom-taxonomy-terms sorted by parent/child-hierarchy?
  91. Listing all term items alphabetically / sorting loop
  92. How to get WordPress term attached to the Post?
  93. Query posts with double taxonomy
  94. Adding predefined terms to a taxonomy
  95. Get parent category id from child category page for custom taxonomy
  96. Submitting Custom Post Types with custom fields from Front-end form
  97. How to get only child terms from a custom taxonomy of current post type?
  98. Custom query – get_the_terms not work
  99. Display different information of a custom post type
  100. Filter posts by tax (dropdown) and meta value
Categories custom-post-types Tags custom-post-types, custom-taxonomy, front-end, post-meta, terms
When ever wordpress home page refreshes the code is not reading the cookie as set anymore
Getting user’s data, by custom field lookup (meta)

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress