WordPress.org

Make WordPress Core

Ticket #17878: taxonomy.diff

File taxonomy.diff, 2.0 KB (added by trepmal, 4 years ago)

wp_remove_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.2.x 
     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_remove_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                        $term_info = wp_insert_term($term, $taxonomy); 
     2197                } 
     2198                if ( is_wp_error($term_info) ) 
     2199                        return $term_info; 
     2200                $term_ids[] = $term_info['term_id']; 
     2201                $tt_id = $term_info['term_taxonomy_id']; 
     2202                $tt_ids[] = $tt_id; 
     2203 
     2204        } 
     2205 
     2206        $delete_terms = $tt_ids; 
     2207        if ( $delete_terms ) { 
     2208                $in_delete_terms = "'" . implode("', '", $delete_terms) . "'"; 
     2209                do_action( 'delete_term_relationships', $object_id, $delete_terms ); 
     2210                $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_delete_terms)", $object_id) ); 
     2211                do_action( 'deleted_term_relationships', $object_id, $delete_terms ); 
     2212                wp_update_term_count($delete_terms, $taxonomy); 
     2213                return true; 
     2214        } 
     2215        return false; 
     2216 
     2217} 
     2218 
     2219/** 
    21632220 * Will make slug unique, if it isn't already. 
    21642221 * 
    21652222 * The $slug has to be unique global to every taxonomy, meaning that one