Skip to content

Instantly share code, notes, and snippets.

@mjangda
Created July 2, 2012 09:12
Show Gist options
  • Save mjangda/3032182 to your computer and use it in GitHub Desktop.
Save mjangda/3032182 to your computer and use it in GitHub Desktop.
Remove multiple terms from a post
<?php
/**
* Helper function since this function doesn't exist in core
*/
function remove_post_terms( $post_id, $terms_to_remove, $taxonomy ) {
$terms_to_remove = array_map( 'intval', $terms_to_remove );
array_filter( $terms_to_remove, function( $term ) { return ! empty( $term ); } );
// Get the existing terms and only keep the ones we don't want removed
$current_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) );
$new_terms = array_diff( $current_terms, $terms_to_remove );
return wp_set_object_terms( $post_id, $new_terms, $taxonomy );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment