WP_Term_Query::get_terms(): WP_Term[]|int[]|string[]|string
- Since
- 4.6.0
- Source
wp-includes/class-wp-term-query.php:348
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
- Scaling
- Scales with input
- Instructions
- 374–410
- Plugin surface
- 6 hooks
- Called by
- 1
Reaches the database via ->get_results().
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 938.
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.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action_ref_array()called directly - querycontent query
get_terms()called directly - cacheobject cache
wp_cache_get()called directly - sqldatabase query
->get_results()called directly - optionoption read or write
get_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.
| When | Instructions | Calls 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) && !$offset | 390–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) && !$offset | 393–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) && !$offset | 395–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) && !$offset | 398–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) && !$offset | 398–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) && !$offset | 401–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) && !$offset | 403–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) && !$offset | 406–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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 938 | 374–410 | 117 | |
| 8.5 | 938 | 374–410 | 117 | |
| 8.4 | 938 | 374–410 | 117 | 36 fewer instructions than PHP 8.3 |
| 8.3 | 974 | 383–419 | 117 | |
| 8.2 | 974 | 383–419 | 117 | |
| 8.1 | 974 | 383–419 | 117 | 8 fewer instructions than PHP 7.4 |
| 7.4 | 982 | 383–419 | 117 |
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:
- apply_filters( list_terms_exclusions )filterline 534 (+186 into the body)
Filters the terms to exclude from the terms query.
- apply_filters( get_terms_fields )filterline 699 (+351 into the body)
Filters the fields to select in the terms query.
- apply_filters( terms_clauses )filterline 731 (+383 into the body)
Filters the terms query SQL clauses.
- apply_filters( terms_pre_query )filterline 773 (+425 into the body)
Filters the terms array before the query takes place.
Uses · 27
- do_action_ref_array()Calls the callback functions that have been added to an action hook, specifying arguments in an array.
- is_taxonomy_hierarchical()Determines whether the taxonomy object is hierarchical.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- _get_term_hierarchy()Retrieves children of taxonomy as term IDs.
- wp_parse_id_list()Cleans up an array, comma- or space-separated list of IDs.
- get_terms()Retrieves the terms in a given taxonomy or list of taxonomies.
- sanitize_term_field()Sanitizes the field value in the term based on the context.
- apply_filters_ref_array()Calls the callback functions that have been added to a filter hook, specifying arguments in an array.
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- wp_list_pluck()Plucks a certain field out of each object or array in an array.
- _prime_term_caches()Adds any terms from the given IDs to the cache that do not already exist in cache.
- wp_cache_set()Saves the data to the cache.
Show all 27
- wp_cache_add()Adds data to the cache, if the cache key doesn't already exist.
- _get_term_children()Gets the subset of $terms that are descendants of $term_id.
- _pad_term_counts()Adds count of children to parent count.
- get_term_children()Merges all term children into a single array of their IDs.
- get_term()Gets all term data from database by term ID.
- wp_lazyload_term_meta()Queue term meta for lazy-loading.
- WP_Term_Query::parse_query()Parse arguments passed to the term query with default query parameters.
- WP_Meta_Query::__construct()Constructor.
- WP_Term_Query::parse_orderby()Parse and sanitize 'orderby' keys passed to the term query.
- WP_Term_Query::parse_order()Parse an 'order' query variable and cast it to ASC or DESC as necessary.
- WP_Term_Query::get_search_sql()Used internally to generate a SQL string related to the 'search' parameter.
- WP_Term_Query::generate_cache_key()Generate cache key.
- WP_Term_Query::populate_terms()Creates an array of term objects from an array of term IDs.
- WP_Term_Query::format_terms()Format response depending on field requested.
- stdClass::__construct()
Used by · 1
- WP_Term_Query::query()Sets up the query and retrieves the results.
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.
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.