Make WordPress Core

Ticket #15475: 15475.diff

File 15475.diff, 2.0 KB (added by ericmann, 14 years ago)

Patch to add wp_unset_object_terms()

  • wp-includes/taxonomy.php

     
    21602160}
    21612161
    21622162/**
     2163 * Remove term(s) associated with a given object.
     2164 *
     2165 * @package WordPress
     2166 * @subpackage Taxonomy
     2167 * @since 3.3
     2168 * @uses $wpdb
     2169 *
     2170 * @param int|array $object_ids The ID(s) of the object(s) to retrieve.
     2171 * @param array|int|string $terms The slug or id of the term to remove.
     2172 * @param string|array $taxonomies The taxonomies to retrieve terms from.
     2173 * @return bool|WP_Error Affected Term IDs
     2174 */
     2175function wp_unset_object_terms( $object_id, $terms, $taxonomy ) {
     2176        global $wpdb;
     2177
     2178        $object_id = (int) $object_id;
     2179
     2180        if ( ! taxonomy_exists($taxonomy) )
     2181                return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
     2182
     2183        if ( !is_array($terms) )
     2184                $terms = array($terms);
     2185
     2186        $tt_ids = array();
     2187        $term_ids = array();
     2188        foreach ( (array) $terms as $term) {
     2189                if ( !strlen(trim($term)) )
     2190                        continue;
     2191
     2192                if ( !$term_info = term_exists($term, $taxonomy) ) {
     2193                        // Skip if a non-existent term ID is passed.
     2194                        if ( is_int($term) )
     2195                                continue;
     2196                }
     2197                if ( is_wp_error($term_info) )
     2198                        return $term_info;
     2199                $term_ids[] = $term_info['term_id'];
     2200                $tt_id = $term_info['term_taxonomy_id'];
     2201                $tt_ids[] = $tt_id;
     2202
     2203        }
     2204
     2205        $delete_terms = $tt_ids;
     2206        if ( $delete_terms ) {
     2207                $in_delete_terms = "'" . implode("', '", $delete_terms) . "'";
     2208                do_action( 'delete_term_relationship', $object_id, $delete_terms );
     2209                $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_delete_terms)", $object_id) );
     2210                do_action( 'deleted_term_relationship', $object_id, $delete_terms );
     2211                wp_update_term_count($delete_terms, $taxonomy);
     2212                return true;
     2213        }
     2214        return false;
     2215}
     2216
     2217/**
    21632218 * Will make slug unique, if it isn't already.
    21642219 *
    21652220 * The $slug has to be unique global to every taxonomy, meaning that one