Created
July 2, 2012 09:12
-
-
Save mjangda/3032182 to your computer and use it in GitHub Desktop.
Remove multiple terms from a post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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