I've got a custom post type set up with custom taxonomies. When a user updates a post I need a way to check to see if there are any location taxonomies applied and if so then remove them.

I've managed to sort the updating part out. So when post is saved it adds the correct Location taxonomy. I'm using two custom taxonomies on the custom post type too. Locations and Industry. So I need to only check the Location taxonomy and remove any that have been set for that specific post while leaving Industry alone.

Any help would be greatly appreciated. I've checked the WP docs site but found I was getting in a muddle with terms, taxonomy etc.

1 Answer 1


I think this could be handled with wp_set_object_terms() call on save_post_{$post->post_type} action. Using the post type specific action you'll only target your cpt.

The action provides bool $update parameter, which can be used to check, if the action is fired for a new post or on update.

Passing an empty $terms value to the function clears any existing terms of the set taxonomy, which saves you the effort of first checking, if any terms exist.

// update action to match your custom post type
add_action( 'save_post_{$post_type}', 'my_clear_post_locations_on_update', 10, 3 );
function my_clear_post_locations_on_update(int $post_ID, WP_Post $post, bool $update) {
  // only affect updates, not new post saves
  if ( ! $update ) {
  // nuke taxonomy terms from the post
  // store return value in a variable for checking the result, if interested
    $post_ID, // object ID
    array(), // terms
    'location', // taxonomy
    false // append

P.S. Replace the empty array with a term slug, single term ID, or array of either term slugs or IDs, if you want to force term/s on the post.

  • Awesome thank you! I just couldn't get my head around it at all. You've made it look really easy! Commented Jul 11, 2022 at 11:37

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.