From 469d8a5e246fe2f948ea48be5cab5be9c6458519 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Fri, 21 Apr 2023 09:24:22 +0000 Subject: [PATCH] Taxonomy: Always lazily load term meta. In [34529] introduced lazy loading of term meta. However, this was only in the context of `WP_Query`. Other parts of the codebase, like `WP_Term_Query` did not lazily load term meta. In this change, calls to `update_termmeta_cache` are now replaced with `wp_lazyload_term_meta`, that instead of priming term meta caches, just adds them to the queue to be primed it ever called. This results in far less database queries, as there a number of places where term meta is being primed unnecessarily and never used. Adding everything to the term meta queue, also means that if term meta is used, that is all loaded in a single database / cache call. Props spacedmonkey, mukesh27, peterwilsoncc. Fixes #57645. Built from https://develop.svn.wordpress.org/trunk@55671 git-svn-id: http://core.svn.wordpress.org/trunk@55183 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-term-query.php | 2 +- wp-includes/post.php | 5 +---- wp-includes/taxonomy.php | 23 ++++++++++++++++++++--- wp-includes/version.php | 2 +- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/wp-includes/class-wp-term-query.php b/wp-includes/class-wp-term-query.php index 0a74f57b9f..d1cc69d96c 100644 --- a/wp-includes/class-wp-term-query.php +++ b/wp-includes/class-wp-term-query.php @@ -865,7 +865,7 @@ class WP_Term_Query { // Prime termmeta cache. if ( $args['update_term_meta_cache'] ) { $term_ids = wp_list_pluck( $term_objects, 'term_id' ); - update_termmeta_cache( $term_ids ); + wp_lazyload_term_meta( $term_ids ); } if ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) ) { diff --git a/wp-includes/post.php b/wp-includes/post.php index 7dc67105e1..3c1af397a3 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -7692,10 +7692,7 @@ function wp_queue_posts_for_term_meta_lazyload( $posts ) { } } - if ( $term_ids ) { - $lazyloader = wp_metadata_lazyloader(); - $lazyloader->queue_objects( 'term', $term_ids ); - } + wp_lazyload_term_meta( $term_ids ); } /** diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index e32afbd2c4..925bcb41ee 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -1426,6 +1426,22 @@ function update_termmeta_cache( $term_ids ) { return update_meta_cache( 'term', $term_ids ); } + +/** + * Queue term meta for lazy-loading. + * + * @since 6.3.0 + * + * @param array $term_ids List of term IDs. + */ +function wp_lazyload_term_meta( array $term_ids ) { + if ( empty( $term_ids ) ) { + return; + } + $lazyloader = wp_metadata_lazyloader(); + $lazyloader->queue_objects( 'term', $term_ids ); +} + /** * Gets all meta data, including meta IDs, for the given term ID. * @@ -4003,6 +4019,7 @@ function _pad_term_counts( &$terms, $taxonomy ) { * * @since 4.6.0 * @since 6.1.0 This function is no longer marked as "private". + * @since 6.3.0 Use wp_lazyload_term_meta() for lazy-loading of term meta. * * @global wpdb $wpdb WordPress database abstraction object. * @@ -4017,10 +4034,10 @@ function _prime_term_caches( $term_ids, $update_meta_cache = true ) { $fresh_terms = $wpdb->get_results( sprintf( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); update_term_cache( $fresh_terms ); + } - if ( $update_meta_cache ) { - update_termmeta_cache( $non_cached_ids ); - } + if ( $update_meta_cache ) { + wp_lazyload_term_meta( $term_ids ); } } diff --git a/wp-includes/version.php b/wp-includes/version.php index c26020a5df..51135f0861 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.3-alpha-55670'; +$wp_version = '6.3-alpha-55671'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.