From 5bc53c066fe1c311e84a332e700dffa216b08e8f Mon Sep 17 00:00:00 2001
From: Barry Ceelen <b@rryceelen.com>
Date: Mon, 9 Nov 2015 00:04:43 +0100
Subject: [PATCH 1/2] Delete term meta when deleting a term

---
 wp-includes/taxonomy-functions.php | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/wp-includes/taxonomy-functions.php b/wp-includes/taxonomy-functions.php
index 174fe74..e916e44 100644
--- a/wp-includes/taxonomy-functions.php
+++ b/wp-includes/taxonomy-functions.php
@@ -2052,7 +2052,7 @@ function wp_delete_object_term_relationships( $object_id, $taxonomies ) {
  * Removes a term from the database.
  *
  * If the term is a parent of other terms, then the children will be updated to
- * that term's parent.
+ * that term's parent. Meta fields associated with the term are 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 +2172,10 @@ 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 ", $tt_id ));
+	foreach ( $term_meta_ids as $mid )
+		delete_metadata_by_mid( 'term', $mid );
+
 	/**
 	 * Fires immediately before a term taxonomy ID is deleted.
 	 *
-- 
2.6.0


From a00a78135ee4ebec2fa8f78c27d2607e19400cf4 Mon Sep 17 00:00:00 2001
From: Barry Ceelen <b@rryceelen.com>
Date: Mon, 9 Nov 2015 00:05:24 +0100
Subject: [PATCH 2/2] Update code comment

---
 wp-includes/meta-functions.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/wp-includes/meta-functions.php b/wp-includes/meta-functions.php
index be7ad5b..14e8af3 100644
--- a/wp-includes/meta-functions.php
+++ b/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.
  */
-- 
2.6.0

