diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 46a25274dd..63c3c844c1 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -2551,6 +2551,9 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) { do_action( 'edited_term_taxonomies', $edit_tt_ids ); } + // Get the term before deleting it or its term relationships so we can pass to actions below. + $deleted_term = get_term( $term, $taxonomy ); + $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); foreach ( (array) $objects as $object ) { @@ -2571,9 +2574,6 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) { foreach ( $tax_object->object_type as $object_type ) clean_object_term_cache( $objects, $object_type ); - // Get the object before deletion so we can pass to actions below - $deleted_term = get_term( $term, $taxonomy ); - /** * Fires immediately before a term taxonomy ID is deleted. * diff --git a/tests/phpunit/tests/term/wpDeleteTerm.php b/tests/phpunit/tests/term/wpDeleteTerm.php new file mode 100644 index 0000000000..064528f281 --- /dev/null +++ b/tests/phpunit/tests/term/wpDeleteTerm.php @@ -0,0 +1,35 @@ +factory->term->create_many( 2, array( + 'taxonomy' => 'wptests_tax', + ) ); + + $p = $this->factory->post->create(); + + wp_set_object_terms( $p, array( $terms[0] ), 'wptests_tax' ); + + add_action( 'delete_term', array( $this, 'catch_deleted_term' ), 10, 4 ); + + wp_delete_term( $terms[0], 'wptests_tax' ); + $this->assertEquals( 1, $this->deleted_term->count ); + + wp_delete_term( $terms[1], 'wptests_tax' ); + $this->assertEquals( 0, $this->deleted_term->count ); + } + + public function catch_deleted_term( $term_id, $tt_id, $taxonomy, $deleted_term ) { + $this->deleted_term = $deleted_term; + } +}