wp_delete_term

Removes a term from the database.

Description

(bool|int|WP_Error) wp_delete_term( (int) $term, (string) $taxonomy, (array) $args = array() ); 

If the term is a parent of other terms, then the children will be updated to that term's parent.

Metadata associated with the term will be deleted.

Returns (bool|int|WP_Error)

True on success, false if term does not exist. Zero on attempted deletion of default Category. WP_Error if the taxonomy does not exist.

Parameters (3)

0. $term (int)
The term.
1. $taxonomy (string)
Taxonomy Name.
2. $args — Optional. (array) => array()
Array of arguments to override the default term ID. Default empty array.

Options

  • default (int) => 0

    The term ID to make the default term. This will only override the terms found if there is only one term found. Any other and the found terms are used.

array(

    /**
     * The term ID to make the default term. This will only override the terms found if there is only one
     * term found. Any other and the found terms are used.
     *
     * @type int
     */
    'default' => 0
);        


Usage

  1. if ( !function_exists( 'wp_delete_term' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // The term. 
  5. $term = -1; 
  6.  
  7. // Taxonomy Name. 
  8. $taxonomy = ''; 
  9.  
  10. // Optional. Array of arguments to override the default term ID. Default empty array. 
  11. $args = array( 
  12. 'default' => 0 
  13. ); 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = wp_delete_term($term, $taxonomy, $args); 
  17.  

Defined (1)

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

/wp-includes/taxonomy.php  
  1. function wp_delete_term( $term, $taxonomy, $args = array() ) { 
  2. global $wpdb; 
  3.  
  4. $term = (int) $term; 
  5.  
  6. if ( ! $ids = term_exists($term, $taxonomy) ) 
  7. return false; 
  8. if ( is_wp_error( $ids ) ) 
  9. return $ids; 
  10.  
  11. $tt_id = $ids['term_taxonomy_id']; 
  12.  
  13. $defaults = array(); 
  14.  
  15. if ( 'category' == $taxonomy ) { 
  16. $defaults['default'] = get_option( 'default_category' ); 
  17. if ( $defaults['default'] == $term ) 
  18. return 0; // Don't delete the default category 
  19.  
  20. $args = wp_parse_args($args, $defaults); 
  21.  
  22. if ( isset( $args['default'] ) ) { 
  23. $default = (int) $args['default']; 
  24. if ( ! term_exists( $default, $taxonomy ) ) { 
  25. unset( $default ); 
  26.  
  27. if ( isset( $args['force_default'] ) ) { 
  28. $force_default = $args['force_default']; 
  29.  
  30. /** 
  31. * Fires when deleting a term, before any modifications are made to posts or terms. 
  32. * @since 4.1.0 
  33. * @param int $term Term ID. 
  34. * @param string $taxonomy Taxonomy Name. 
  35. */ 
  36. do_action( 'pre_delete_term', $term, $taxonomy ); 
  37.  
  38. // Update children to point to new parent 
  39. if ( is_taxonomy_hierarchical($taxonomy) ) { 
  40. $term_obj = get_term($term, $taxonomy); 
  41. if ( is_wp_error( $term_obj ) ) 
  42. return $term_obj; 
  43. $parent = $term_obj->parent; 
  44.  
  45. $edit_ids = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id ); 
  46. $edit_tt_ids = wp_list_pluck( $edit_ids, 'term_taxonomy_id' ); 
  47.  
  48. /** 
  49. * Fires immediately before a term to delete's children are reassigned a parent. 
  50. * @since 2.9.0 
  51. * @param array $edit_tt_ids An array of term taxonomy IDs for the given term. 
  52. */ 
  53. do_action( 'edit_term_taxonomies', $edit_tt_ids ); 
  54.  
  55. $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) ); 
  56.  
  57. // Clean the cache for all child terms. 
  58. $edit_term_ids = wp_list_pluck( $edit_ids, 'term_id' ); 
  59. clean_term_cache( $edit_term_ids, $taxonomy ); 
  60.  
  61. /** 
  62. * Fires immediately after a term to delete's children are reassigned a parent. 
  63. * @since 2.9.0 
  64. * @param array $edit_tt_ids An array of term taxonomy IDs for the given term. 
  65. */ 
  66. do_action( 'edited_term_taxonomies', $edit_tt_ids ); 
  67.  
  68. // Get the term before deleting it or its term relationships so we can pass to actions below. 
  69. $deleted_term = get_term( $term, $taxonomy ); 
  70.  
  71. $object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); 
  72.  
  73. foreach ( $object_ids as $object_id ) { 
  74. $terms = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids', 'orderby' => 'none' ) ); 
  75. if ( 1 == count($terms) && isset($default) ) { 
  76. $terms = array($default); 
  77. } else { 
  78. $terms = array_diff($terms, array($term)); 
  79. if (isset($default) && isset($force_default) && $force_default) 
  80. $terms = array_merge($terms, array($default)); 
  81. $terms = array_map('intval', $terms); 
  82. wp_set_object_terms( $object_id, $terms, $taxonomy ); 
  83.  
  84. // Clean the relationship caches for all object types using this term. 
  85. $tax_object = get_taxonomy( $taxonomy ); 
  86. foreach ( $tax_object->object_type as $object_type ) 
  87. clean_object_term_cache( $object_ids, $object_type ); 
  88.  
  89. $term_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE term_id = %d ", $term ) ); 
  90. foreach ( $term_meta_ids as $mid ) { 
  91. delete_metadata_by_mid( 'term', $mid ); 
  92.  
  93. /** 
  94. * Fires immediately before a term taxonomy ID is deleted. 
  95. * @since 2.9.0 
  96. * @param int $tt_id Term taxonomy ID. 
  97. */ 
  98. $wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) ); 
  99.  
  100. /** 
  101. * Fires immediately after a term taxonomy ID is deleted. 
  102. * @since 2.9.0 
  103. * @param int $tt_id Term taxonomy ID. 
  104. */ 
  105.  
  106. // Delete the term if no taxonomies use it. 
  107. if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) ) 
  108. $wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) ); 
  109.  
  110. clean_term_cache($term, $taxonomy); 
  111.  
  112. /** 
  113. * Fires after a term is deleted from the database and the cache is cleaned. 
  114. * @since 2.5.0 
  115. * @since 4.5.0 Introduced the `$object_ids` argument. 
  116. * @param int $term Term ID. 
  117. * @param int $tt_id Term taxonomy ID. 
  118. * @param string $taxonomy Taxonomy slug. 
  119. * @param mixed $deleted_term Copy of the already-deleted term, in the form specified 
  120. * by the parent function. WP_Error otherwise. 
  121. * @param array $object_ids List of term object IDs. 
  122. */ 
  123. do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term, $object_ids ); 
  124.  
  125. /** 
  126. * Fires after a term in a specific taxonomy is deleted. 
  127. * The dynamic portion of the hook name, `$taxonomy`, refers to the specific 
  128. * taxonomy the term belonged to. 
  129. * @since 2.3.0 
  130. * @since 4.5.0 Introduced the `$object_ids` argument. 
  131. * @param int $term Term ID. 
  132. * @param int $tt_id Term taxonomy ID. 
  133. * @param mixed $deleted_term Copy of the already-deleted term, in the form specified 
  134. * by the parent function. WP_Error otherwise. 
  135. * @param array $object_ids List of term object IDs. 
  136. */ 
  137. do_action( "delete_{$taxonomy}", $term, $tt_id, $deleted_term, $object_ids ); 
  138.  
  139. return true;