Make WordPress Core

Opened 6 years ago

Closed 6 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:


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)

36846.patch (1.8 KB) - added by ramiy 6 years ago.

Download all attachments as: .zip

Change History (3)

6 years ago

#1 @ramiy
6 years ago

  • Keywords has-patch dev-feedback 2nd-opinion added

#2 @boonebgorges
6 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

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 only slug and name will result in meaningful performance benefits, I think it's going to be exceedingly rare. In contrast, use of fields causes a lot of confusion with respect to caching: we currently keep separate copies of term queries in the cache for each potential value of fields. This pollutes the cache, by potentially multiplying the cache footprint of get_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 to get_terms().

If you're able to demonstrate that SELECT slug, name ... is more than a microoptimization over SELECT * in a real-world situation, we can reconsider the proposal.

Note: See TracTickets for help on using tickets.