1695 | | $tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids')); |
1696 | | $in_tt_ids = "'" . implode("', '", $tt_ids) . "'"; |
1697 | | do_action( 'delete_term_relationships', $object_id, $tt_ids ); |
1698 | | $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id) ); |
1699 | | do_action( 'deleted_term_relationships', $object_id, $tt_ids ); |
1700 | | wp_update_term_count($tt_ids, $taxonomy); |
| 1693 | $term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) ); |
| 1694 | $term_ids = array_map( 'intval', $term_ids ); |
| 1695 | wp_remove_object_terms( $object_id, $term_ids, $taxonomy ); |
2174 | | $delete_terms = array_diff($old_tt_ids, $tt_ids); |
2175 | | if ( $delete_terms ) { |
2176 | | $in_delete_terms = "'" . implode("', '", $delete_terms) . "'"; |
2177 | | do_action( 'delete_term_relationships', $object_id, $delete_terms ); |
2178 | | $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_delete_terms)", $object_id) ); |
2179 | | do_action( 'deleted_term_relationships', $object_id, $delete_terms ); |
2180 | | wp_update_term_count($delete_terms, $taxonomy); |
| 2170 | $delete_tt_ids = array_diff($old_tt_ids, $tt_ids); |
| 2171 | |
| 2172 | if ( $delete_tt_ids ) { |
| 2173 | $in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'"; |
| 2174 | $delete_term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM $wpdb->term_taxonomy AS tt WHERE tt.taxonomy = %s AND tt.term_taxonomy_id IN ($in_delete_tt_ids)", $taxonomy ) ); |
| 2175 | $delete_term_ids = array_map( 'intval', $delete_term_ids ); |
| 2176 | |
| 2177 | $remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy ); |
| 2178 | if ( is_wp_error( $remove ) ) |
| 2179 | return $remove; |
| 2200 | * Add term association(s) to a given object. |
| 2201 | * |
| 2202 | * @package WordPress |
| 2203 | * @subpackage Taxonomy |
| 2204 | * @since 3.4 |
| 2205 | * @uses wp_set_object_terms |
| 2206 | * |
| 2207 | * @param int $object_id The object to relate to. |
| 2208 | * @param array|int|string $terms The slug or id of the term, will append to all existing |
| 2209 | * related terms in this taxonomy. |
| 2210 | * @param array|string $taxonomy The context in which to relate the term to the object. |
| 2211 | * @return array|WP_Error Affected Term IDs |
| 2212 | */ |
| 2213 | function wp_add_object_terms( $object_id, $terms, $taxonomy ) { |
| 2214 | return wp_set_object_terms( $object_id, $terms, $taxonomy, true); |
| 2215 | } |
| 2216 | |
| 2217 | /** |
| 2218 | * Remove term(s) associated with a given object. |
| 2219 | * |
| 2220 | * @package WordPress |
| 2221 | * @subpackage Taxonomy |
| 2222 | * @since 3.4 |
| 2223 | * @uses $wpdb |
| 2224 | * |
| 2225 | * @param int|array $object_ids The ID(s) of the object(s) to retrieve. |
| 2226 | * @param array|int|string $terms The slug or id of the term to remove. |
| 2227 | * @param array|string $taxonomy The context in which to relate the term to the object. |
| 2228 | * @return bool|WP_Error True on success, false or WP_Error on failure. |
| 2229 | */ |
| 2230 | function wp_remove_object_terms( $object_id, $terms, $taxonomy ) { |
| 2231 | global $wpdb; |
| 2232 | |
| 2233 | $object_id = (int) $object_id; |
| 2234 | |
| 2235 | if ( ! taxonomy_exists( $taxonomy ) ) |
| 2236 | return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) ); |
| 2237 | |
| 2238 | if ( ! is_array( $terms ) ) |
| 2239 | $terms = array( $terms ); |
| 2240 | |
| 2241 | $tt_ids = array(); |
| 2242 | |
| 2243 | foreach ( (array) $terms as $term) { |
| 2244 | if ( ! strlen( trim( $term ) ) ) |
| 2245 | continue; |
| 2246 | |
| 2247 | if ( ! $term_info = term_exists( $term, $taxonomy ) ) { |
| 2248 | // Skip if a non-existent term ID is passed. |
| 2249 | if ( is_int( $term ) ) |
| 2250 | continue; |
| 2251 | } |
| 2252 | |
| 2253 | if ( is_wp_error( $term_info ) ) |
| 2254 | return $term_info; |
| 2255 | |
| 2256 | $tt_ids[] = $term_info['term_taxonomy_id']; |
| 2257 | } |
| 2258 | |
| 2259 | if ( $tt_ids ) { |
| 2260 | $in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'"; |
| 2261 | do_action( 'delete_term_relationships', $object_id, $tt_ids ); |
| 2262 | $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) ); |
| 2263 | do_action( 'deleted_term_relationships', $object_id, $tt_ids ); |
| 2264 | wp_update_term_count( $tt_ids, $taxonomy ); |
| 2265 | return true; |
| 2266 | } |
| 2267 | |
| 2268 | return false; |
| 2269 | } |
| 2270 | |
| 2271 | /** |