0

I am developing a import plug in where I would upload a text file which contain all sorts of data about vehicle and that would insert that data in custom post type vehicle.

Now I am having problem inserting a category and its subcategory.

Like I have term audi and a term a3 I would like to add audi as category and a3 as its subcategory and assign to that post.

I tried

wp_set_object_terms( $news_arr[0], array(utf8_encode($news_arr[2]),$news_arr[3]), 'vehicle_models', $append = false ) ;

where $news_arr[2] and $news_arr[3] are audi and a3 respectively.

I also tried wp_insert_terms and set it parent as id of the first set object terms that creates a subcategory but does not assign to that post.

any help will be appreciated.

thanks.

1

I figured it out first set the terms to post through wp_set_object_terms and then run a update query in term_taxonomy table and set parent id of the the second term to first term.

$taxo = wp_set_object_terms( $news_arr[0], utf8_encode($news_arr[2]), 'vehicle_models', $append = false ) ;
$child = wp_set_object_terms( $news_arr[0], utf8_encode($news_arr[3]), 'vehicle_models', $append = true ) ;
$category_parent    = get_term_by('name', $news_arr[2], 'vehicle_models');
                    $category_child     = get_term_by('name', $news_arr[3], 'vehicle_models');
                    $category_parent_id = $category_parent->term_id;
                    $category_child_id  = $category_child->term_id;
                    $wpdb->update( 
                    $wpdb->prefix.'term_taxonomy', 
                    array( 
                        'parent' => $category_parent_id,

), array( 'term_id' => $category_child_id ) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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