• Resolved Manny Fleurmond

    (@funkatronic)


    I’m having issues with adding hierarchical terms in a taxonomy. I’ve created a taxonomy for my site that describes types of media releases:

    $labels = array(
                'name'=> _x('Release Types/Regions', 'taxonomy general name' ),
                'singular_name' => _x('Release Type/Region', 'taxonomy singular name'),
                'search_items' => __('Search Release Types/Regions'),
                'popular_items' => __('Popular Release Types/Regions'),
                'all_items' => __('All Release Types/Regions'),
                'edit_item' => __('Edit Release Type/Regions'),
                'edit_item' => __('Edit Release Type/Region'),
                'update_item' => __('Update Release Type/Region'),
                'add_new_item' => __('Add New Release Type/Region'),
                'new_item_name' => __('New Release Type/Region Name'),
                'separate_items_with_commas' => __('Seperate Release Types/Regions with Commas'),
                'add_or_remove_items' => __('Add or Remove Release Types/Regions'),
                'choose_from_most_used' => __('Choose from Most Used Release Types/Regions')
            );
            $args = array(
                'hierarchical' =>true,
                'labels' => $labels,
                'query_var' => true,
                'rewrite' => array('slug' =>'release_type')
            );
            register_taxonomy('veda_release_type', 'veda_release',$args); $labels = array(
                'name'=> _x('Release Types/Regions', 'taxonomy general name' ),
                'singular_name' => _x('Release Type/Region', 'taxonomy singular name'),
                'search_items' => __('Search Release Types/Regions'),
                'popular_items' => __('Popular Release Types/Regions'),
                'all_items' => __('All Release Types/Regions'),
                'edit_item' => __('Edit Release Type/Regions'),
                'edit_item' => __('Edit Release Type/Region'),
                'update_item' => __('Update Release Type/Region'),
                'add_new_item' => __('Add New Release Type/Region'),
                'new_item_name' => __('New Release Type/Region Name'),
                'separate_items_with_commas' => __('Seperate Release Types/Regions with Commas'),
                'add_or_remove_items' => __('Add or Remove Release Types/Regions'),
                'choose_from_most_used' => __('Choose from Most Used Release Types/Regions')
            );
            $args = array(
                'hierarchical' =>true,
                'labels' => $labels,
                'query_var' => true,
                'rewrite' => array('slug' =>'release_type')
            );
            register_taxonomy('veda_release_type', 'veda_release',$args);

    I’m using term_exists and wp_insert_terms to check and see if the term exists and then insert them. The terms are hierarchical and are as follows:

    DVD
    –Region 0
    –Region 1
    –Region 2
    –Region 3
    –Region 4
    –Region 5
    –Region 6
    Blu-Ray
    –Region A
    –Region B
    –Region C

    The problem isn’t that the terms aren’t inserted correctly: they are. If I inspect the MySQL tables for the wp_terms and the wp_term_taxonomy tables, not only are the terms inserted to the correct taxonomy, the child subterms have the proper parents.

    The Problem I’m having is that despite being in the table, the page that lists the terms only lists the two main terms and none of the children. There is an exception: in the area of the screen where you can insert new terms, under the dropdown to choose a parent, all the child terms are visible and you are able to select one as a parent for a new term. Not only that, if you do select one and create a child of one of the children and then refresh the screen, the child terms THEN magically show up.
    Here is a screen cap showing that the child terms show up in the dropdown but not the main tax screen
    I seriously think there is something wrong with how WP implements its taxonomy system. Can anyone replicate this?

    Here is the complete code:

    //Release Type and Region
            $labels = array(
                'name'=> _x('Release Types/Regions', 'taxonomy general name' ),
                'singular_name' => _x('Release Type/Region', 'taxonomy singular name'),
                'search_items' => __('Search Release Types/Regions'),
                'popular_items' => __('Popular Release Types/Regions'),
                'all_items' => __('All Release Types/Regions'),
                'edit_item' => __('Edit Release Type/Regions'),
                'edit_item' => __('Edit Release Type/Region'),
                'update_item' => __('Update Release Type/Region'),
                'add_new_item' => __('Add New Release Type/Region'),
                'new_item_name' => __('New Release Type/Region Name'),
                'separate_items_with_commas' => __('Seperate Release Types/Regions with Commas'),
                'add_or_remove_items' => __('Add or Remove Release Types/Regions'),
                'choose_from_most_used' => __('Choose from Most Used Release Types/Regions')
            );
            $args = array(
                'hierarchical' =>true,
                'labels' => $labels,
                'query_var' => true,
                'rewrite' => array('slug' =>'release_type')
            );
            register_taxonomy('veda_release_type', 'veda_release',$args);
    
    function insert_term ($term, $taxonomy, $args = array()) {
            if (isset($args['parent'])) {
                $parent = $args['parent'];
            } else {
                $parent = 0;
            }
            $result = term_exists($term, $taxonomy, $parent);
            if ($result == false || $result == 0) {
                return wp_insert_term($term, $taxonomy, $args);
            } else {
                return (array) $result;
            }
    }
    $dvd = insert_term('DVD','veda_release_type');
        insert_term('Region 0','veda_release_type',array('parent'=>$dvd['term_id']));
        insert_term('Region 1','veda_release_type',array('parent'=>$dvd['term_id']));
        insert_term('Region 2','veda_release_type',array('parent'=>$dvd['term_id']));
        insert_term('Region 3','veda_release_type',array('parent'=>$dvd['term_id']));
        insert_term('Region 4','veda_release_type',array('parent'=>$dvd['term_id']));
        insert_term('Region 5','veda_release_type',array('parent'=>$dvd['term_id']));
        insert_term('Region 6','veda_release_type',array('parent'=>$dvd['term_id']));
    
        $bd = insert_term('Blu-Ray', 'veda_release_type');
        insert_term('Region A','veda_release_type',array('parent'=>$bd['term_id']));
        insert_term('Region B','veda_release_type',array('parent'=>$bd['term_id']));
        insert_term('Region C','veda_release_type',array('parent'=>$bd['term_id']));

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’m having the same exact issue.

    Thread Starter Manny Fleurmond

    (@funkatronic)

    Forgot I created this.

    I found a solution via Stack Exchange. Basically, its a cache bug. What I found was that the terms actually are saved but for some reason don’t show up because the cache that displays them isn’t updated. To solve this, after you insert the terms, you use this code:

    delete_option("{taxonomy}_children");

    Where you could replace {taxonomy} with the name of your taxonomy. What this does is clear the cache (which is saved as an option) and forces WordPress to pull your terms from the database.

    Special thanks to scribu for finding the solution, found here: https://wordpress.stackexchange.com/questions/8357/inserting-terms-in-an-hierarchical-taxonomy

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problems inserting terms in a hierarchical taxonomy’ is closed to new replies.