On my site I have a Custom Post Type "listing" which has a hierarchial Taxonomy "local".

The terms in this taxonomy are in the form of States > Cities.

For example "Washington" is a parent term & Arlington, Auburn, Davenport etc could be it's child taxonomies.

Now when I export & import this Custom Post Type to another site, my child level taxonomies become top level taxonomies, they no longer retain the hierarchy.

I have used code in functions.php to create these, no plugins. What am I doing wrong?

  • where is your code ? Commented Aug 27, 2013 at 7:23

1 Answer 1


I understand your problem and had to work around it as well: wp_insert_term() doesn't handle parent/children connections.

You can check this with the following:

$tax = 'category';
$terms = get_terms( array( $tax ) );

$children_to_update = array();

foreach ( $terms as $term )
    $checked_term = get_term_by( 'name', $term, $tax );

    // ... retrieve parent here ...

    if ( ! $checked_term )
        $term_data = wp_insert_term(
            array( 'parent' => $parent )
        if (
            is_array( $term_data )
            AND ! is_wp_error( $term_data )
            AND '0' !== $parent
            $children_to_update[ $parent ][] = $term_data['term_id'];
// inspect result
var_dump( $children_to_update );

You will see that it stays empty. The reason is simple: There's an Option that holds this information. You'll need to update it as well:

$option = get_option( "{$tax}_children" );
foreach( $children_to_update as $new_p => $new_c )
    if ( ! array_key_exists( $new_p, $option ) )
        $option[ $new_p ] = $new_c;
        foreach ( $new_c as $c )
            if ( ! in_array( $c, $option[ $new_p ] ) )
                $option[ $new_p ][] = $c;
update_option( "{$tax}_children", $option );

At first I didn't get that the hierarchy didn't work, but when you look at the taxonomy overview page in the admin UI, then you'll see that the terms are not aligned like the should (in child/parent lists).

  • Thanks for your answer. I just imported some blog posts with hierarchial categories and they all turned into top level Categories as well! Where and how can I use the code that you have provided? Commented Aug 27, 2013 at 10:20
  • Ehm... haven't you written a custom importer? Is this a problem with the default importer?
    – kaiser
    Commented Aug 27, 2013 at 12:01
  • Yes this is the problem with default importer. To test I made 2 test sites, created few sample posts under Hierarchical Categories on one site, export-imported them into another site and I see the Category Hierarchy is lost! I had no idea WP did this? Can anyone test this phenomenon & prove that I am not crazy! Commented Aug 27, 2013 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.