Make WordPress Core


Ignore:
Timestamp:
09/11/2023 11:35:59 AM (20 months ago)
Author:
spacedmonkey
Message:

Taxonomy: Cache term objects in WP_Term_Query if query is filtered.

When utilizing the terms_clauses or get_terms_fields filters within WP_Term_Query and the selected fields are modified, the entire term object is now cached. This adjustment is necessary because filters can broaden the selected fields beyond just the term ID. Fields linked to the term object, such as the count or parent, may undergo modifications when queried. Caching the complete object ensures the accurate storage of these modified fields within the cache.

Props spacedmonkey, tnolte, peterwilsoncc.
Fixes #58116.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-term-query.php

    r55671 r56555  
    739739        $limits   = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
    740740
     741        $fields_is_filtered = implode( ', ', $selects ) !== $fields;
     742
    741743        if ( $where ) {
    742744            $where = "WHERE $where";
     
    783785            } elseif ( 'count' !== $_fields ) {
    784786                if ( ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) )
    785                     || ( 'all' === $_fields && $args['pad_counts'] )
     787                    || ( 'all' === $_fields && $args['pad_counts'] || $fields_is_filtered )
    786788                ) {
    787789                    $term_ids = wp_list_pluck( $cache, 'term_id' );
     
    885887                $term_cache[]    = $object;
    886888            }
     889        } elseif ( $fields_is_filtered ) {
     890            $term_cache = $term_objects;
    887891        } else {
    888892            $term_cache = wp_list_pluck( $term_objects, 'term_id' );
Note: See TracChangeset for help on using the changeset viewer.