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
This commit is contained in:
spacedmonkey 2023-04-21 09:24:22 +00:00
parent 769915b890
commit 469d8a5e24
4 changed files with 23 additions and 9 deletions

View File

@ -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'] ) ) {

View File

@ -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 );
}
/**

View File

@ -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 );
}
}

View File

@ -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.