I am trying to remove a string off of a bunch of slugs in terms from a CPT taxonomy.
For example, I have a term named "Accounting" but the slug has the "accounting-expert-witnesses" I just need to remove the "-expert-witnesses" part of the slug.
I am using this post as a reference as it appears to be what am I looking for but I cannot seem to get any of the suggestions to work: How to update taxonomy custom field with wp_update_term()?
I have tried using update_term_meta, update_post_meta, wp_update_term and none of them are doing what I need.
The closest I've got is using update_term_meta and it appears to be working when I error log the result however when I look at the terms in the dashboard nothing has been updated.
I can't figure out what I'm doing wrong here?
function remove_stuff() {
$terms = get_terms( array(
'taxonomy' => 'experts_industries',
'hide_empty' => false,
) );
foreach($terms as $term) {
$newSlug = str_replace('-expert-witnesses', '' , $term->slug);
//update_term_meta($term->id, 'slug', $newSlug);
//update_post_meta($term->id, 'slug', $newSlug);
wp_update_term($term->id, 'experts_industries', array( 'slug' => $newSlug ));
error_log($term->slug);
}
}
add_action( 'wp_loaded', 'remove_stuff' );
wp_update_term
would the right function to use. A slug isn't meta, and a term isn't a post, so those 2 functions are not appropriate. Are you certainwp_update_term
did not work? Are you sure you're not still looking at cached data?experts_industries
is the correct taxonomy name? It shouldn't be hyphenated?