0

I have a page where admin assigns term in new taxonomy and delete from the taxonomy it is already assigned to.

Like I have a vehicle post type and there are a bunch of entries in vehicle extra.

Now admin would decide that he wants to enter airbag in security feature instead of vehicle extra it is already assigned.

So terms entry under vehicle extra should be deleted and new terms entry should be made under security feature.

wp_update_term( 3164, 'vehicle_interior_feature' );

where 3164 is the term ID already present and vehicle_interior_feature that is the new taxonomy. While vehicle_extras is the old taxonomy this term is assigned to.

  • I have tried wp_update_terms but that does not delete or add new entry. and there is no option in wp_insert_terms that would allow for new entries and delete previous term – apoorv Oct 19 '16 at 13:10
  • Please, show us what you've tried. – Max Yudin Oct 19 '16 at 13:19
  • wp_update_term( 3164, 'vehicle_interior_feature' ) where 3164 is the term id already present and vehicle_interior_feature that is the new taxonomy. while vehicle_extras is the old taxonomy this term is assigned to – apoorv Oct 19 '16 at 13:26
  • I've made your job. See the updated question and How to ask a good question. – Max Yudin Oct 19 '16 at 13:48
1

wp_update_term() doesn't changes taxonomy. It just updated the existing taxonomy. Say the below code-

$update = wp_update_term( 1, 'category', array(
    'name' => 'Uncategorized Renamed',
    'slug' => 'uncategorized-renamed'
) );

if ( ! is_wp_error( $update ) ) {
    echo 'Success!';
}

This code finds the category which ID is 1, then updates it to the name and slug passed by as a parameter. In the context of my system the category with ID 1 is Uncategorized. So it will rename it.

For changing terms taxonomy there is no default function. Here I've written one for you. Take a look below-

function the_dramatist_change_terms_taxonomy( $term_id, $future_taxonomy ){
    global $wpdb;
    $update = $wpdb->update(
        $wpdb->prefix . 'term_taxonomy',
        [ 'taxonomy' => $future_taxonomy ],
        [ 'term_taxonomy_id' => $term_id ],
        [ '%s' ],
        [ '%d' ]
    );
    return $update;
}

Here $term_id is the term's ID which you taxonomy you wanna change and $future_taxonomy is the terms future taxonomy. $future_taxonomy must have to be string like 'category', 'post_tag' or 'vehicle_interior_feature'. It actually updates the database value directly. So be careful before you use it. Specially careful if your term has any parent. Cause it is basically updating the taxonomy value at wp_terms_taxonomy table, not any other one. For updating terms taxonomy I've not found any better option.

Hope this code help you.

  • Thank you Dramatist that worked I had that in the back of my mind to run a custom query with $wpdb but I was thinking of a native wordpress solution like wp_insert_terms. – apoorv Oct 20 '16 at 6:39
  • Native WordPress solution will be like deleting the term from taxonomy from one then adding it to another with same name. And the hassel doesn't end here. After deleting and adding the taxonomy you also will be need to assign the new term to all the posts it was assigned before deleting. So I think it's better if you can accomplish the task with a single query instead of running all those queries. And another thing, if you think this answer have helped you, it would be great if you accept the answer. @apoorv – CodeMascot Oct 20 '16 at 9:59

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.