From 1db4b029c303a5486de77b588e258b6afd0e4c8c Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 19 Feb 2021 14:31:08 +0000 Subject: [PATCH] Taxonomy: Optimize `wp_delete_term()` for large object counts without a default term. When deleting a term, it has to be removed individually from each object it's connected to, which can take some significant time when there are a lot of objects. By calling `wp_remove_object_terms()` when no default term is required, we can skip the terms fetch/diff step and significantly speed up the deletion process. Props dd32. Fixes #52549. Built from https://develop.svn.wordpress.org/trunk@50389 git-svn-id: http://core.svn.wordpress.org/trunk@50000 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/taxonomy.php | 7 +++++++ wp-includes/version.php | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index b250e0feef..c8633d8ea2 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -1943,6 +1943,11 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) { $object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); foreach ( $object_ids as $object_id ) { + if ( ! isset( $default ) ) { + wp_remove_object_terms( $object_id, $term, $taxonomy ); + continue; + } + $terms = wp_get_object_terms( $object_id, $taxonomy, @@ -1951,6 +1956,7 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) { 'orderby' => 'none', ) ); + if ( 1 === count( $terms ) && isset( $default ) ) { $terms = array( $default ); } else { @@ -1959,6 +1965,7 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) { $terms = array_merge( $terms, array( $default ) ); } } + $terms = array_map( 'intval', $terms ); wp_set_object_terms( $object_id, $terms, $taxonomy ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index b78769ef4b..16d1c862e0 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.7-beta3-50388'; +$wp_version = '5.7-beta3-50389'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.