wppaste
WordPress

WP_Term_Query::get_terms(): WP_Term[]|int[]|string[]|string

Since
4.6.0
Source
wp-includes/class-wp-term-query.php:348
Retrieves the query results.

Description

The return type varies depending on the value passed to $args['fields'].

The following will result in an array of WP_Term objects being returned:

  • 'all'
  • 'all_with_object_id'

The following will result in a numeric string being returned:

  • 'count'

The following will result in an array of text strings being returned:

  • 'id=>name'
  • 'id=>slug'
  • 'names'
  • 'slugs'

The following will result in an array of numeric strings being returned:

  • 'id=>parent'

The following will result in an array of integers being returned:

  • 'ids'
  • 'tt_ids'

Compatibility

WordPress
since 4.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

WP_Term[]|int[]|string[]|string
Array of terms, or number of terms as numeric string when 'count' is passed to $args['fields'].

Performance profile

How much work a call to WP_Term_Query::get_terms() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->get_results().

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
374–410

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 938.

Plugin surface
6 hooks

Third-party callbacks on 'pre_get_terms', 'get_terms_args', 'list_terms_exclusions' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action_ref_array()called directly
  • querycontent queryget_terms()called directly
  • cacheobject cachewp_cache_get()called directly
  • sqldatabase query->get_results()called directly
  • optionoption read or writeget_option()one call below WP_Term_Query::get_terms()

Further down the call graph this can also reach serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 16 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Term_Query::get_terms() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms)374–391->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), ->format_terms()
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms)377–392->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), wp_list_pluck(), ->format_terms()
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms)379–396->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), wp_cache_add(), ->format_terms()
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms)382–397->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), wp_list_pluck(), wp_cache_add(), ->format_terms()
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms)382–399->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), wp_list_pluck(), wp_lazyload_term_meta(), ->format_terms()
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms)385–400->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), wp_list_pluck(), wp_lazyload_term_meta(), wp_list_pluck(), ->format_terms()
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms)387–404->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), wp_list_pluck(), wp_lazyload_term_meta(), wp_cache_add(), ->format_terms()
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms) && is_array($term_objects) && !$offset390–396->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), array_slice(), ->format_terms()
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms)390–405->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), wp_list_pluck(), wp_lazyload_term_meta(), wp_list_pluck(), wp_cache_add(), ->format_terms()
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms) && is_array($term_objects) && !$offset393–397->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), array_slice(), wp_list_pluck(), ->format_terms()
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms) && is_array($term_objects) && !$offset395–401->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), array_slice(), wp_cache_add(), ->format_terms()
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms) && is_array($term_objects) && !$offset398–402->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), array_slice(), wp_list_pluck(), wp_cache_add(), ->format_terms()
4 further outcomes, up to 410 instructions
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms) && is_array($term_objects) && !$offset398–404->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), array_slice(), wp_list_pluck(), wp_lazyload_term_meta(), ->format_terms()
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms) && is_array($term_objects) && !$offset401–405->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), array_slice(), wp_list_pluck(), wp_lazyload_term_meta(), wp_list_pluck(), ->format_terms()
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms) && is_array($term_objects) && !$offset403–409->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), array_slice(), wp_list_pluck(), wp_lazyload_term_meta(), wp_cache_add(), ->format_terms()
$_orderby !== "term_order" && empty($include) && empty($inclusions) && empty($exclude_tree) && empty($exclude) && empty($exclusions) && $parent === "" && empty($meta_clauses) && empty($value) && !isset($clauses) && $_fields !== "count" && !empty($terms) && is_array($term_objects) && !$offset406–410->parse_query(), ->parse_query_vars(), apply_filters(), ->parse_orderby(), ->parse_order(), apply_filters(), array_map(), array_map(), array_map(), ->parse_query_vars(), ->get_sql(), ->get_clauses(), apply_filters(), compact(), apply_filters(), ->get_results(), wp_list_pluck(), _prime_term_caches(), ->populate_terms(), array_slice(), wp_list_pluck(), wp_lazyload_term_meta(), wp_list_pluck(), wp_cache_add(), ->format_terms()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev938374–410117
8.5938374–410117
8.4938374–41011736 fewer instructions than PHP 8.3
8.3974383–419117
8.2974383–419117
8.1974383–4191178 fewer instructions than PHP 7.4
7.4982383–419117

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 6

6 hooks fire while WP_Term_Query::get_terms() runs, in this order:

  1. do_action( pre_get_terms )actionline 365 (+17 into the body)

    Fires before terms are retrieved.

  2. apply_filters( get_terms_args )filterline 408 (+60 into the body)

    Filters the terms query arguments.

  3. apply_filters( list_terms_exclusions )filterline 534 (+186 into the body)

    Filters the terms to exclude from the terms query.

  4. apply_filters( get_terms_fields )filterline 699 (+351 into the body)

    Filters the fields to select in the terms query.

  5. apply_filters( terms_clauses )filterline 731 (+383 into the body)

    Filters the terms query SQL clauses.

  6. apply_filters( terms_pre_query )filterline 773 (+425 into the body)

    Filters the terms array before the query takes place.

Uses · 27

Show all 27

Used by · 1

Source code

	public function get_terms() {		global $wpdb; 		$this->parse_query( $this->query_vars );		$args = &$this->query_vars; 		// Set up meta_query so it's available to 'pre_get_terms'.		$this->meta_query = new WP_Meta_Query();		$this->meta_query->parse_query_vars( $args ); 		/**		 * Fires before terms are retrieved.		 *		 * @since 4.6.0		 *		 * @param WP_Term_Query $query Current instance of WP_Term_Query (passed by reference).		 */		do_action_ref_array( 'pre_get_terms', array( &$this ) ); 		$taxonomies = (array) $args['taxonomy']; 		// Save queries by not crawling the tree in the case of multiple taxes or a flat tax.		$has_hierarchical_tax = false;		if ( $taxonomies ) {			foreach ( $taxonomies as $_tax ) {				if ( is_taxonomy_hierarchical( $_tax ) ) {					$has_hierarchical_tax = true;				}			}		} else {			// When no taxonomies are provided, assume we have to descend the tree.			$has_hierarchical_tax = true;		} 		if ( ! $has_hierarchical_tax ) {			$args['hierarchical'] = false;			$args['pad_counts']   = false;		} 		// 'parent' overrides 'child_of'.		if ( 0 < (int) $args['parent'] ) {			$args['child_of'] = false;		} 		if ( 'all' === $args['get'] ) {			$args['childless']    = false;			$args['child_of']     = 0;			$args['hide_empty']   = 0;			$args['hierarchical'] = false;			$args['pad_counts']   = false;		} 		/**		 * Filters the terms query arguments.		 *		 * @since 3.1.0		 *		 * @param array    $args       An array of get_terms() arguments.		 * @param string[] $taxonomies An array of taxonomy names.		 */		$args = apply_filters( 'get_terms_args', $args, $taxonomies ); 		// Avoid the query if the queried parent/child_of term has no descendants.		$child_of = $args['child_of'];		$parent   = $args['parent']; 		if ( $child_of ) {			$_parent = $child_of;		} elseif ( $parent ) {			$_parent = $parent;		} else {			$_parent = false;		} 		if ( $_parent ) {			$in_hierarchy = false;			foreach ( $taxonomies as $_tax ) {				$hierarchy = _get_term_hierarchy( $_tax ); 				if ( isset( $hierarchy[ $_parent ] ) ) {

Changelog

Introduced in 4.6.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/class-wp-term-query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.