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.
Built from https://develop.svn.wordpress.org/trunk@56555


git-svn-id: http://core.svn.wordpress.org/trunk@56067 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2023-09-11 11:37:19 +00:00
parent 4920adb477
commit de170c0c2e
2 changed files with 6 additions and 2 deletions

View File

@ -738,6 +738,8 @@ class WP_Term_Query {
$order = isset( $clauses['order'] ) ? $clauses['order'] : '';
$limits = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
$fields_is_filtered = implode( ', ', $selects ) !== $fields;
if ( $where ) {
$where = "WHERE $where";
}
@ -782,7 +784,7 @@ class WP_Term_Query {
$cache = array_map( 'intval', $cache );
} elseif ( 'count' !== $_fields ) {
if ( ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) )
|| ( 'all' === $_fields && $args['pad_counts'] )
|| ( 'all' === $_fields && $args['pad_counts'] || $fields_is_filtered )
) {
$term_ids = wp_list_pluck( $cache, 'term_id' );
} else {
@ -884,6 +886,8 @@ class WP_Term_Query {
$object->count = $term->count;
$term_cache[] = $object;
}
} elseif ( $fields_is_filtered ) {
$term_cache = $term_objects;
} else {
$term_cache = wp_list_pluck( $term_objects, 'term_id' );
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.4-alpha-56554';
$wp_version = '6.4-alpha-56555';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.