diff --git a/wp-includes/class-wp-term-query.php b/wp-includes/class-wp-term-query.php index 65b35289a9..fc6e950c21 100644 --- a/wp-includes/class-wp-term-query.php +++ b/wp-includes/class-wp-term-query.php @@ -91,6 +91,7 @@ class WP_Term_Query { * @since 4.9.0 Added 'slug__in' support for 'orderby'. * @since 5.1.0 Introduced the 'meta_compare_key' parameter. * @since 5.3.0 Introduced the 'meta_type_key' parameter. + * @since 6.4.0 Introduced the 'cache_results' parameter. * * @param string|array $query { * Optional. Array or query string of term query parameters. Default empty. @@ -178,6 +179,7 @@ class WP_Term_Query { * Default false. * @type string $cache_domain Unique cache key to be produced when this query is stored in * an object cache. Default 'core'. + * @type bool $cache_results Whether to cache term information. Default true. * @type bool $update_term_meta_cache Whether to prime meta caches for matched terms. Default true. * @type string|string[] $meta_key Meta key or keys to filter by. * @type string|string[] $meta_value Meta value or values to filter by. @@ -220,6 +222,7 @@ class WP_Term_Query { 'parent' => '', 'childless' => false, 'cache_domain' => 'core', + 'cache_results' => true, 'update_term_meta_cache' => true, 'meta_query' => '', 'meta_key' => '', @@ -776,41 +779,47 @@ class WP_Term_Query { return $this->terms; } - $cache_key = $this->generate_cache_key( $args, $this->request ); - $cache = wp_cache_get( $cache_key, 'term-queries' ); + if ( $args['cache_results'] ) { + $cache_key = $this->generate_cache_key( $args, $this->request ); + $cache = wp_cache_get( $cache_key, 'term-queries' ); - if ( false !== $cache ) { - if ( 'ids' === $_fields ) { - $cache = array_map( 'intval', $cache ); - } elseif ( 'count' !== $_fields ) { - if ( ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) ) + if ( false !== $cache ) { + if ( 'ids' === $_fields ) { + $cache = array_map( 'intval', $cache ); + } elseif ( 'count' !== $_fields ) { + if ( ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) ) || ( 'all' === $_fields && $args['pad_counts'] || $fields_is_filtered ) - ) { - $term_ids = wp_list_pluck( $cache, 'term_id' ); - } else { - $term_ids = array_map( 'intval', $cache ); + ) { + $term_ids = wp_list_pluck( $cache, 'term_id' ); + } else { + $term_ids = array_map( 'intval', $cache ); + } + + _prime_term_caches( $term_ids, $args['update_term_meta_cache'] ); + + $term_objects = $this->populate_terms( $cache ); + $cache = $this->format_terms( $term_objects, $_fields ); } - _prime_term_caches( $term_ids, $args['update_term_meta_cache'] ); - - $term_objects = $this->populate_terms( $cache ); - $cache = $this->format_terms( $term_objects, $_fields ); + $this->terms = $cache; + return $this->terms; } - - $this->terms = $cache; - return $this->terms; } if ( 'count' === $_fields ) { $count = $wpdb->get_var( $this->request ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared - wp_cache_set( $cache_key, $count, 'term-queries' ); + if ( $args['cache_results'] ) { + wp_cache_set( $cache_key, $count, 'term-queries' ); + } return $count; } $terms = $wpdb->get_results( $this->request ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared if ( empty( $terms ) ) { - wp_cache_add( $cache_key, array(), 'term-queries' ); + if ( $args['cache_results'] ) { + wp_cache_add( $cache_key, array(), 'term-queries' ); + } return array(); } @@ -892,7 +901,10 @@ class WP_Term_Query { $term_cache = wp_list_pluck( $term_objects, 'term_id' ); } - wp_cache_add( $cache_key, $term_cache, 'term-queries' ); + if ( $args['cache_results'] ) { + wp_cache_add( $cache_key, $term_cache, 'term-queries' ); + } + $this->terms = $this->format_terms( $term_objects, $_fields ); return $this->terms; @@ -1153,7 +1165,7 @@ class WP_Term_Query { // $args can be anything. Only use the args defined in defaults to compute the key. $cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ); - unset( $cache_args['update_term_meta_cache'] ); + unset( $cache_args['cache_results'], $cache_args['update_term_meta_cache'] ); if ( 'count' !== $args['fields'] && 'all_with_object_id' !== $args['fields'] ) { $cache_args['fields'] = 'all'; diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 3eb36552e6..a611307b6d 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -1541,8 +1541,7 @@ function term_exists( $term, $taxonomy = '', $parent_term = null ) { // Ensure that while importing, queries are not cached. if ( ! empty( $_wp_suspend_cache_invalidation ) ) { - // @todo Disable caching once #52710 is merged. - $defaults['cache_domain'] = microtime(); + $defaults['cache_results'] = false; } if ( ! empty( $taxonomy ) ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index 729ccbbed7..6dfc7393ba 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-alpha-56584'; +$wp_version = '6.4-alpha-56585'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.