0

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' );
14
  • are you trying to partially update the value? Or are you setting an entire new value? Where is the code that displays your meta in the dashboard? Are you updating or are you adding meta?
    – Tom J Nowell
    Commented Jun 30, 2022 at 20:54
  • I updated my question to try and better articulate what I'm trying to do and what I've tried. I hope that helps. Thanks. Commented Jun 30, 2022 at 21:14
  • 1
    If you're trying to change the slug of a term across the board, 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 certain wp_update_term did not work? Are you sure you're not still looking at cached data?
    – vancoder
    Commented Jun 30, 2022 at 21:19
  • Yeah I've refreshed the page and ran the code no less than a dozen times. Nothing is getting updated. Is there something wrong in here: wp_update_term($term->id, 'experts_industries', array( 'slug' => $newSlug )); this is starting to drive me nuts. Commented Jun 30, 2022 at 21:26
  • 1
    Possibly a stupid question but you're sure experts_industries is the correct taxonomy name? It shouldn't be hyphenated?
    – vancoder
    Commented Jun 30, 2022 at 22:01

1 Answer 1

0

A terms slug is not stored as meta/custom fields, and it's definitely not post meta, so update_term_meta and update_post_meta are innapropriate and can never be used to achieve your goal.

The correct function is wp_update_term, however you've used that incorrectly too:

  • $term->id isn't a thing, if we look at the WP_Term class and the examples on the official docs, we see $term->term_id is what should be used
  • there is no code checking the return value of wp_update_term, if it failed there would have been a WP_Error object with an error message explaining what happened.
  • error_log($term->slug); doesn't fetch the new slug from the database or make a new query to check for a new slug, this cannot be used to test if it worked. You need to check the return value of wp_update_term instead

From the official dev docs:

Return

(array|WP_Error) An array containing the term_id and term_taxonomy_id, WP_Error otherwise.

More examples and information at:

3
  • Thanks. By all accounts, this should work. However it does not work with any of the custom post-type taxonomies, it always returns "invalid taxonomy" works great with default categories taxonomy on the posts content type though. Idk. I'm giving up, going to just update all the slugs manually I guess. Thanks anyway. Commented Jun 30, 2022 at 22:53
  • if you're running the code before the taxonomies are registered or on a request were the taxonomies are not registered then it will fail. Likewise if the capitalisation is different. Taxonomies and post types should be registered on the init hook. Also if you've not changed id to term_id then your taxonomy will never match the term as the term will always be null or 0
    – Tom J Nowell
    Commented Jun 30, 2022 at 23:42
  • It was because of the action hook priority. The person who set up the action hook to create cpt taxonomies had set the priority way low and my code was executing before the cpt code was resulting in the invalid taxonomy error. The code in your answer remains correct. Thanks again I appreciate the help. Commented Jul 1, 2022 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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