Opened 9 years ago
Closed 9 years ago
#36846 closed defect (bug) (wontfix)
Allow get_terms() to return slug&name fields
Reported by: | ramiy | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Taxonomy | Keywords: | has-patch dev-feedback 2nd-opinion |
Focuses: | performance | Cc: |
Description
When we use the get_terms() function, we can choose not to return the complete term objects but a smaller array with predefined fields (for performance).
it's done using fields
argument:
$terms = get_terms( array( 'taxonomy' => 'post_tag', 'fields' => 'all', ) );
Currently, the fields
argument supports the following:
all
- returns an array of complete term objects. (DEFAULT)ids
- returns an array of ids.names
- returns an array of term names.count
- returns the number of matching terms.id=>parent
- returns an associative array with ids as keys, parent term IDs as values.id=>name
- returns an associative array with ids as keys, term names as values.id=>slug
- returns an associative array with ids as keys, term slugs as values.
The problem is that in a growing number of websites I need to return only term-slugs and term-names.
My patch adds another variation to the fields
argument:
slug=>name
- returns an associative array with term slugs as keys, term names as values.
Attachments (1)
Change History (3)
Note: See
TracTickets for help on using
tickets.
Hi @ramiy - Thanks for the patch.
I'm against adding new values to
fields
. While there may be a narrow set of cases where selecting onlyslug
andname
will result in meaningful performance benefits, I think it's going to be exceedingly rare. In contrast, use offields
causes a lot of confusion with respect to caching: we currently keep separate copies of term queries in the cache for each potential value offields
. This pollutes the cache, by potentially multiplying the cache footprint ofget_terms()
by three or four times what it actually has to be.A better strategy is for the main terms query to be for IDs only, and the term objects fetched individually from the 'terms' cache. This work was begun in #34239 for
get_the_terms()
, and the patches on #35381 will extend the strategy toget_terms()
.If you're able to demonstrate that
SELECT slug, name ...
is more than a microoptimization overSELECT *
in a real-world situation, we can reconsider the proposal.