Make WordPress Core

03/10/2022 10:56:09 AM (2 years ago)

Taxonomy: Only store term_ids and object_ids in WP_Term_Query query caches.

The query cache currently implemented in WP_Term_Query caches the final output of the query, depending on what fields are requested. This is wasteful, as if a user requests fields => all, then an unlimited array of WP_Term objects could be stored in the object cache. Instead of storing the whole WP_Term object, this change only the term_id is stored. To get an array the full WP_Term objects, the _prime_term_caches function is called with an array of ids. In instances where a persistent object cache is not in use, then this will result in another SQL query to be run. After _prime_term_caches is called if this term is requested again in the same page load, then it will already be loaded into memory. If a user runs WP_Term_Query with the fields param set to all_with_object_id, an array of objects containing both the term_id and object_ids are stored in cache.

This change also improves the logic to load term meta caches. This change ensures that term meta is always primed for all terms loaded in the term query.

Props Spacedmonkey, boonebgorges, jbpaul17, peterwilsoncc, flixos90, pbearne.
Fixes #37189.

1 edited


  • trunk/tests/phpunit/tests/term/getTerms.php

    r52010 r52836  
    119119        $time1 = wp_cache_get( 'last_changed', 'terms' );
    120120        $this->assertNotEmpty( $time1 );
    121         $this->assertSame( $num_queries + 1, $wpdb->num_queries );
     121        $this->assertSame( $num_queries + 2, $wpdb->num_queries );
    123123        $num_queries = $wpdb->num_queries;
Note: See TracChangeset for help on using the changeset viewer.