[wp-trac] [WordPress Trac] #37198: Use `WP_Term_Query` for `wp_get_object_terms()`

WordPress Trac noreply at wordpress.org
Tue Sep 27 18:56:28 UTC 2016


#37198: Use `WP_Term_Query` for `wp_get_object_terms()`
-----------------------------------+------------------
 Reporter:  flixos90               |       Owner:
     Type:  enhancement            |      Status:  new
 Priority:  normal                 |   Milestone:  4.7
Component:  Taxonomy               |     Version:  4.6
 Severity:  normal                 |  Resolution:
 Keywords:  has-patch 2nd-opinion  |     Focuses:
-----------------------------------+------------------

Comment (by flixos90):

 Replying to [comment:7 boonebgorges]:
 > - I'm enforcing the 'term_count' thing by changing the value of 'order'
 to something legal before passing it to `parse_orderby()`

 I think it's `term_order`, other than that it appears to be good solution.
 :)

 > So, fun fact, this change adds caching to all `wp_get_object_terms()`
 calls. Yet we still have a separate `{$taxonomy}_relationships` cache. We
 can't easily tear the latter bit out, because it's scattered through the
 codebase. Plus, it's a more durable cache than the `WP_Term_Query` cache,
 because `WP_Term_Query` is based on `last_changed`, which will be bumped
 pretty frequently. Still, having two layers of cache makes me uneasy. I
 wrote a test that demonstrates that things are working OK. Do you have
 thoughts about it, @flixos90 ?

 That's interesting. I don't think it will be a problem since the caches
 shouldn't interfere with each other. Also, the `{$taxonomy}_relationships`
 cache has a more specific use-case (terms of _one_ taxonomy for _one_
 object) and it is actually built using `wp_get_object_terms()`, so I think
 we can ignore it here. They both have their use-case.

 What we could theoretically do is actually make use of that cache in
 `wp_get_object_terms()`:
 * check if _one_ object ID and _one_ taxonomy is provided and if
 `$orderby` doesn't include `term_order`
 * if so, check the `{$taxonomy}_relationships` cache for that object ID
 using `get_object_term_cache()`
 * if the cache brings a successful response, we adjust the query
 arguments:
     * remove the `object_ids` argument
     * add the term IDs to a `include` argument
 * send the (possibly modified) arguments to a `WP_Term_Query`

 If any of the above checks fails, we proceed as usual instead. I'm not
 sure if that actually makes sense performance-wise, but just wanted to
 mention it as a possibility. When having the relationships cached, this
 would allow for much simpler queries (and I assume there _are_ many cases
 where terms for one object ID and one taxonomy are requested.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/37198#comment:8>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list