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 taxonomy you wanna change and $future_taxonomy
is the terms future taxonomy. $future_taxonomy
must have to be string like ‘category’, ‘post_tag’ or ‘any_other_taxonomy’. 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.
And for inserting terms to a taxonomy you can use wp_insert_term( $term, $taxonomy, $args = array() )
. So you can check that if desired taxonomy is exist or not. If it exists then update it, if it is not then create it. Like below-
$term = term_exists( 'Uncategorized', 'category' );
if ( $term !== 0 && $term !== null ) {
the_dramatist_change_terms_taxonomy( $term->term_id, $future_taxonomy )
} else {
wp_insert_term( '...All..The...Parameter..Here' );
}