wp_remove_object_terms

Remove term(s) associated with a given object.

Description

(bool|WP_Error) wp_remove_object_terms( (int) $object_id, (array|int|string) $terms, (array|string) $taxonomy ); 

Returns (bool|WP_Error)

True on success, false or WP_Error on failure.

Parameters (3)

0. $object_id (int)
The ID of the object from which the terms will be removed.
1. $terms (array|int|string)
The slug(s) or ID(s) of the term(s) to remove.
2. $taxonomy (array|string)
Taxonomy name.

Usage

  1. if ( !function_exists( 'wp_remove_object_terms' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // The ID of the object from which the terms will be removed. 
  5. $object_id = -1; 
  6.  
  7. // The slug(s) or ID(s) of the term(s) to remove. 
  8. $terms = null; 
  9.  
  10. // Taxonomy name. 
  11. $taxonomy = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wp_remove_object_terms($object_id, $terms, $taxonomy); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/taxonomy.php  
  1. function wp_remove_object_terms( $object_id, $terms, $taxonomy ) { 
  2. global $wpdb; 
  3.  
  4. $object_id = (int) $object_id; 
  5.  
  6. if ( ! taxonomy_exists( $taxonomy ) ) { 
  7. return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); 
  8.  
  9. if ( ! is_array( $terms ) ) { 
  10. $terms = array( $terms ); 
  11.  
  12. $tt_ids = array(); 
  13.  
  14. foreach ( (array) $terms as $term ) { 
  15. if ( ! strlen( trim( $term ) ) ) { 
  16. continue; 
  17.  
  18. if ( ! $term_info = term_exists( $term, $taxonomy ) ) { 
  19. // Skip if a non-existent term ID is passed. 
  20. if ( is_int( $term ) ) { 
  21. continue; 
  22.  
  23. if ( is_wp_error( $term_info ) ) { 
  24. return $term_info; 
  25.  
  26. $tt_ids[] = $term_info['term_taxonomy_id']; 
  27.  
  28. if ( $tt_ids ) { 
  29. $in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'"; 
  30.  
  31. /** 
  32. * Fires immediately before an object-term relationship is deleted. 
  33. * @since 2.9.0 
  34. * @since 4.7.0 Added the `$taxonomy` parameter. 
  35. * @param int $object_id Object ID. 
  36. * @param array $tt_ids An array of term taxonomy IDs. 
  37. * @param string $taxonomy Taxonomy slug. 
  38. */ 
  39. do_action( 'delete_term_relationships', $object_id, $tt_ids, $taxonomy ); 
  40. $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) ); 
  41.  
  42. wp_cache_delete( $object_id, $taxonomy . '_relationships' ); 
  43. wp_cache_delete( 'last_changed', 'terms' ); 
  44.  
  45. /** 
  46. * Fires immediately after an object-term relationship is deleted. 
  47. * @since 2.9.0 
  48. * @since 4.7.0 Added the `$taxonomy` parameter. 
  49. * @param int $object_id Object ID. 
  50. * @param array $tt_ids An array of term taxonomy IDs. 
  51. * @param string $taxonomy Taxonomy slug. 
  52. */ 
  53. do_action( 'deleted_term_relationships', $object_id, $tt_ids, $taxonomy ); 
  54.  
  55. wp_update_term_count( $tt_ids, $taxonomy ); 
  56.  
  57. return (bool) $deleted; 
  58.  
  59. return false;