Make WordPress Core


Ignore:
Timestamp:
04/21/2023 09:22:04 AM (3 years ago)
Author:
spacedmonkey
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r55526 r55671  
    14271427}
    14281428
     1429
     1430/**
     1431 * Queue term meta for lazy-loading.
     1432 *
     1433 * @since 6.3.0
     1434 *
     1435 * @param array $term_ids List of term IDs.
     1436 */
     1437function wp_lazyload_term_meta( array $term_ids ) {
     1438    if ( empty( $term_ids ) ) {
     1439        return;
     1440    }
     1441    $lazyloader = wp_metadata_lazyloader();
     1442    $lazyloader->queue_objects( 'term', $term_ids );
     1443}
     1444
    14291445/**
    14301446 * Gets all meta data, including meta IDs, for the given term ID.
     
    40044020 * @since 4.6.0
    40054021 * @since 6.1.0 This function is no longer marked as "private".
     4022 * @since 6.3.0 Use wp_lazyload_term_meta() for lazy-loading of term meta.
    40064023 *
    40074024 * @global wpdb $wpdb WordPress database abstraction object.
     
    40184035
    40194036        update_term_cache( $fresh_terms );
    4020 
    4021         if ( $update_meta_cache ) {
    4022             update_termmeta_cache( $non_cached_ids );
    4023         }
     4037    }
     4038
     4039    if ( $update_meta_cache ) {
     4040        wp_lazyload_term_meta( $term_ids );
    40244041    }
    40254042}
Note: See TracChangeset for help on using the changeset viewer.