[wp-trac] [WordPress Trac] #56055: Wrong cache created in WP_Term_Query class
WordPress Trac
noreply at wordpress.org
Thu Jun 23 15:28:56 UTC 2022
#56055: Wrong cache created in WP_Term_Query class
--------------------------+-----------------------------
Reporter: kubiq | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Taxonomy | Version: 6.0
Severity: critical | Keywords:
Focuses: |
--------------------------+-----------------------------
When you call the same `get_terms( $args )` multiple times, then the
second (or every other call) will return wrong results - they will contain
also empty terms
You can easily test this on any taxonomy with some empty term
{{{#!php
<?php
echo '1. call:';
echo '<pre>' . print_r( get_terms(array( 'taxonomy' => 'product_cat',
'parent' => 0 )), 1 ) . '</pre>';
echo '2. call:';
echo '<pre>' . print_r( get_terms(array( 'taxonomy' => 'product_cat',
'parent' => 0 )), 1 ) . '</pre>';
}}}
logically the output should be same in the 1.call and also in 2. call, but
it's not
**SOLUTION:**
In `get_terms` function inside `WP_Term_Query` class at the end there is
`wp_cache_add( $cache_key, $terms, 'terms' );`
but `$terms` variable contains all terms even if you have `hide_empty` set
to `TRUE`
*`$term_objects` is the correct variable that contains correct terms and
it is also used in next lines
it can be fixed by getting terms ids from `$term_objects` variable eg.
{{{#!php
<?php
$correct_terms = array_map(function( $term ){ return (object)array(
'term_id' => $term->term_id ); }, $term_objects);
}}}
and replace it in cache function
{{{#!php
<?php
wp_cache_add( $cache_key, $correct_terms, 'terms' );
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/56055>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list