Wordpress - Inserting terms in an Hierarchical Taxonomy

The hierarchy is cached and it's not invalidated properly. This is a bug, still unresolved as of WP 3.1.

A workaround would be to call delete_option("{$taxonomy}_children"); directly.

See the _get_term_hierarchy() function.


My 2 cents - I am building an extension for the WooCommerce plugin that allows catalog category/product management via an uploaded CSV file. I ran into this same issue when generating the categories. The solution to clear the cached WooCommerce categories was to call delete_option("product_cat_children"); after the loop that creates the categories and sub-categories. Hope this helps someone else!


You could possibly be receiving an object back from term_exists($term, $taxonomy, $parent) if the taxonomy is set when called. It's set in the sample code you posted, so i'm assuming that's the issue.

Your inserts statements are expecting an array when they reference keys..

Eg.

$bd['term_id']
$dvd['term_id']

..but you may actually be getting an object back from term_exists.

A quick way to determine if that's the problem would be to update this line.

return $result;

to..

return is_object( $result ) ? (array) $result : $result;

If i'm right and that is the problem, that should fix it, though i'd not call that an elegant fix.

Hopefully that's the answer/help you were looking for.. :)