diff --git a/src/wp-includes/meta-functions.php b/src/wp-includes/meta-functions.php index be7ad5b65a..29cf291f92 100644 --- a/src/wp-includes/meta-functions.php +++ b/src/wp-includes/meta-functions.php @@ -558,7 +558,7 @@ function metadata_exists( $meta_type, $object_id, $meta_key ) { * * @global wpdb $wpdb WordPress database abstraction object. * - * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) + * @param string $meta_type Type of object metadata is for (e.g., comment, post, meta, or user). * @param int $meta_id ID for a specific meta row * @return object|false Meta object or false. */ @@ -692,7 +692,7 @@ function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = * * @global wpdb $wpdb WordPress database abstraction object. * - * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) + * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). * @param int $meta_id ID for a specific meta row * @return bool True on successful delete, false on failure. */ diff --git a/src/wp-includes/taxonomy-functions.php b/src/wp-includes/taxonomy-functions.php index 174fe7427d..035f18b0d4 100644 --- a/src/wp-includes/taxonomy-functions.php +++ b/src/wp-includes/taxonomy-functions.php @@ -2054,6 +2054,8 @@ function wp_delete_object_term_relationships( $object_id, $taxonomies ) { * 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. + * * The `$args` 'default' will only override the terms found, if there is only one * term found. Any other and the found terms are used. * @@ -2172,6 +2174,11 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) { foreach ( $tax_object->object_type as $object_type ) clean_object_term_cache( $objects, $object_type ); + $term_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE term_id = %d ", $term ) ); + foreach ( $term_meta_ids as $mid ) { + delete_metadata_by_mid( 'term', $mid ); + } + /** * Fires immediately before a term taxonomy ID is deleted. * diff --git a/tests/phpunit/tests/term/meta.php b/tests/phpunit/tests/term/meta.php index 6c34cab918..f6c7b903bf 100644 --- a/tests/phpunit/tests/term/meta.php +++ b/tests/phpunit/tests/term/meta.php @@ -381,6 +381,24 @@ class Tests_Term_Meta extends WP_UnitTestCase { $this->assertSame( 'ambiguous_term_id', $found->get_error_code() ); } + /** + * @ticket 34626 + */ + public function test_term_meta_should_be_deleted_when_term_is_deleted() { + $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); + + add_term_meta( $t, 'foo', 'bar' ); + add_term_meta( $t, 'foo1', 'bar' ); + + $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) ); + $this->assertSame( 'bar', get_term_meta( $t, 'foo1', true ) ); + + $this->assertTrue( wp_delete_term( $t, 'wptests_tax' ) ); + + $this->assertSame( '', get_term_meta( $t, 'foo', true ) ); + $this->assertSame( '', get_term_meta( $t, 'foo1', true ) ); + } + public static function set_cache_results( $q ) { $q->set( 'cache_results', true ); }