Created
December 21, 2011 15:11
-
-
Save mjangda/1506353 to your computer and use it in GitHub Desktop.
Remove a given term from the specified post. This function is missing from core WordPress.
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 | |
/** | |
* Remove a given term from the specified post | |
* | |
* Helper function since this functionality doesn't exist in core | |
*/ | |
function my_remove_post_term( $post_id, $term, $taxonomy ) { | |
if ( ! is_numeric( $term ) ) { | |
$term = get_term( $term, $taxonomy ); | |
if ( ! $term || is_wp_error( $term ) ) | |
return false; | |
$term_id = $term->term_id; | |
} else { | |
$term_id = $term; | |
} | |
// Get the existing terms and only keep the ones we don't want removed | |
$new_terms = array(); | |
$current_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) ); | |
foreach ( $current_terms as $current_term ) { | |
if ( $current_term != $term_id ) | |
$new_terms[] = intval( $current_term ); | |
} | |
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