Make WordPress Core


Ignore:
Timestamp:
03/10/2022 10:56:09 AM (2 years ago)
Author:
spacedmonkey
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/getTermBy.php

    r52010 r52836  
    55 */
    66class Tests_Term_GetTermBy extends WP_UnitTestCase {
     7
     8    protected $query = '';
    79
    810    public function test_get_term_by_slug() {
     
    124126        $num_queries = $wpdb->num_queries;
    125127        $found       = get_term_by( 'slug', 'foo', 'wptests_tax' );
    126         $num_queries++;
     128        $num_queries = $num_queries + 2;
    127129
    128130        $this->assertInstanceOf( 'WP_Term', $found );
     
    210212     */
    211213    public function test_query_should_contain_limit_clause() {
    212         global $wpdb;
    213 
    214214        $term_id = $this->factory->term->create(
    215215            array(
     
    218218            )
    219219        );
    220         $found   = get_term_by( 'name', 'burrito', 'post_tag' );
    221         $this->assertSame( $term_id, $found->term_id );
    222         $this->assertStringContainsString( 'LIMIT 1', $wpdb->last_query );
     220        add_filter( 'terms_pre_query', array( $this, 'get_query_from_filter' ), 10, 2 );
     221        $found = get_term_by( 'name', 'burrito', 'post_tag' );
     222        $this->assertSame( $term_id, $found->term_id );
     223        $this->assertStringContainsString( 'LIMIT 1', $this->query );
    223224    }
    224225
     
    283284        $this->assertFalse( $found_by_name );
    284285    }
     286
     287    public function get_query_from_filter( $terms, $wp_term_query ) {
     288        $this->query = $wp_term_query->request;
     289
     290        return $terms;
     291    }
    285292}
Note: See TracChangeset for help on using the changeset viewer.