[wp-trac] [WordPress Trac] #49799: get_the_terms() object term cache check too strict?
WordPress Trac
noreply at wordpress.org
Fri Apr 3 15:24:14 UTC 2020
#49799: get_the_terms() object term cache check too strict?
--------------------------+------------------------------------------------
Reporter: dingo_d | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Taxonomy | Version: 5.4
Severity: normal | Keywords: dev-feedback 2nd-opinion has-patch
Focuses: |
--------------------------+------------------------------------------------
I ran into a weird issue when doing integration tests on one of a client's
project.
I was setting up custom post and custom taxonomy from the factory (for
posts I used the default one, and for custom taxonomy I have created my
own factory that extends `WP_UnitTest_Factory_For_Term`), set everything
up, but the part of the plugins code that used `get_the_terms` returned
nothing. So after racking my brains, I tried `wp_get_post_terms`, and lo
and behold, this worked (the test posts had assigned terms for them, I
checked before invoking my function I needed to test using
`get_the_terms`).
So something was up with caching. I started to dig and it turned out that
{{{#!php
$terms = get_object_term_cache( $post->ID, $taxonomy );
}}}
inside `wp-includes/category-template.php`
returned an empty array.
Now, since the line 1246 says
{{{#!php
if ( false === $terms ) {
}}}
The empty array, while being a ''falsy'' value, it's not a `false` value
(https://3v4l.org/RVuG7), so the above check will return false and the
returned value from the `get_the_terms` function will be `false`.
So when I 'loosened' the condition in my test suite WP, the `$terms =
wp_get_object_terms( $post->ID, $taxonomy );`
works, and I get my terms inside the tests.
If the condition is loosened, nothing seems to be lost in terms of
checking the emptiness of the terms from the term object cache (if it's
full, it's `false` all the times https://3v4l.org/34UQk).
Now, on the face value, this could be the oddity of the test suite (which
in my case is set up using wpcli scaffold command), but I'm thinking that
changing this requirement cannot hurt (from what I can tell) in the long
run.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/49799>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list