wppaste
WordPress

WP_REST_Terms_Controller::get_items( WP_REST_Request $request ): WP_REST_Response|WP_Error

Since
4.7.0, 6.8.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php:235
Retrieves terms associated with a taxonomy.

Compatibility

WordPress
since 6.8.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.

Parameters

$requestWP_REST_Request
Full details about the request.

Return value

WP_REST_Response|WP_Error
Response object on success, or WP_Error object on failure.

Performance profile

How much work a call to WP_REST_Terms_Controller::get_items() 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 wp_get_object_terms().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
113–178

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

Plugin surface
1 hook

Third-party callbacks on 'rest_{$this->taxonomy}_query' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent querywp_get_object_terms()called directly

Further down the call graph this can also reach option, cache, 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 · 24 distinct outcomes

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

WhenInstructionsCalls it makes
!isset($prepared_args) && !isset($registered) && empty($prepared_args) && !$page113–137->get_collection_params(), get_taxonomy(), ->is_method(), apply_filters(), get_terms(), wp_count_terms(), taxonomy(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg()
!isset($prepared_args) && !isset($registered) && empty($prepared_args) && !$page116–136->get_collection_params(), get_taxonomy(), ->is_method(), apply_filters(), get_terms(), wp_count_terms(), rest_ensure_response(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg()
!isset($prepared_args) && !isset($registered) && !empty($prepared_args) && !$page123–147->get_collection_params(), get_taxonomy(), ->is_method(), apply_filters(), wp_get_object_terms(), wp_count_terms(), taxonomy(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg()
!isset($prepared_args) && !isset($registered) && empty($prepared_args) && isset($taxonomy_obj) && !$page124–143->get_collection_params(), get_taxonomy(), array_merge(), ->is_method(), apply_filters(), get_terms(), wp_count_terms(), taxonomy(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg()
!isset($prepared_args) && !isset($registered) && empty($prepared_args)124–151->get_collection_params(), get_taxonomy(), ->is_method(), apply_filters(), get_terms(), wp_count_terms(), taxonomy(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && !empty($prepared_args) && !$page126–146->get_collection_params(), get_taxonomy(), ->is_method(), apply_filters(), wp_get_object_terms(), wp_count_terms(), rest_ensure_response(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg()
!isset($prepared_args) && !isset($registered) && empty($prepared_args) && isset($taxonomy_obj) && !$page127–142->get_collection_params(), get_taxonomy(), array_merge(), ->is_method(), apply_filters(), get_terms(), wp_count_terms(), rest_ensure_response(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg()
!isset($prepared_args) && !isset($registered) && empty($prepared_args)127–150->get_collection_params(), get_taxonomy(), ->is_method(), apply_filters(), get_terms(), wp_count_terms(), rest_ensure_response(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && isset($taxonomy_obj) && !$page134–153->get_collection_params(), get_taxonomy(), array_merge(), ->is_method(), apply_filters(), wp_get_object_terms(), wp_count_terms(), taxonomy(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg()
!isset($prepared_args) && !isset($registered) && !empty($prepared_args)134–161->get_collection_params(), get_taxonomy(), ->is_method(), apply_filters(), wp_get_object_terms(), wp_count_terms(), taxonomy(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && empty($prepared_args) && isset($taxonomy_obj)135–157->get_collection_params(), get_taxonomy(), array_merge(), ->is_method(), apply_filters(), get_terms(), wp_count_terms(), taxonomy(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && isset($taxonomy_obj) && !$page137–152->get_collection_params(), get_taxonomy(), array_merge(), ->is_method(), apply_filters(), wp_get_object_terms(), wp_count_terms(), rest_ensure_response(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg()
12 further outcomes, up to 177 instructions
!isset($prepared_args) && !isset($registered) && !empty($prepared_args)137–160->get_collection_params(), get_taxonomy(), ->is_method(), apply_filters(), wp_get_object_terms(), wp_count_terms(), rest_ensure_response(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && empty($prepared_args) && $page137–162->get_collection_params(), get_taxonomy(), ->is_method(), apply_filters(), get_terms(), wp_count_terms(), taxonomy(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && empty($prepared_args) && isset($taxonomy_obj)138–156->get_collection_params(), get_taxonomy(), array_merge(), ->is_method(), apply_filters(), get_terms(), wp_count_terms(), rest_ensure_response(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && empty($prepared_args) && $page140–161->get_collection_params(), get_taxonomy(), ->is_method(), apply_filters(), get_terms(), wp_count_terms(), rest_ensure_response(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && isset($taxonomy_obj)145–167->get_collection_params(), get_taxonomy(), array_merge(), ->is_method(), apply_filters(), wp_get_object_terms(), wp_count_terms(), taxonomy(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && !empty($prepared_args) && $page147–172->get_collection_params(), get_taxonomy(), ->is_method(), apply_filters(), wp_get_object_terms(), wp_count_terms(), taxonomy(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && isset($taxonomy_obj)148–166->get_collection_params(), get_taxonomy(), array_merge(), ->is_method(), apply_filters(), wp_get_object_terms(), wp_count_terms(), rest_ensure_response(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && empty($prepared_args) && isset($taxonomy_obj) && $page148–168->get_collection_params(), get_taxonomy(), array_merge(), ->is_method(), apply_filters(), get_terms(), wp_count_terms(), taxonomy(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && !empty($prepared_args) && $page150–171->get_collection_params(), get_taxonomy(), ->is_method(), apply_filters(), wp_get_object_terms(), wp_count_terms(), rest_ensure_response(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && empty($prepared_args) && isset($taxonomy_obj) && $page151–167->get_collection_params(), get_taxonomy(), array_merge(), ->is_method(), apply_filters(), get_terms(), wp_count_terms(), rest_ensure_response(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && isset($taxonomy_obj) && $page158–178->get_collection_params(), get_taxonomy(), array_merge(), ->is_method(), apply_filters(), wp_get_object_terms(), wp_count_terms(), taxonomy(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header()
!isset($prepared_args) && !isset($registered) && isset($taxonomy_obj) && $page161–177->get_collection_params(), get_taxonomy(), array_merge(), ->is_method(), apply_filters(), wp_get_object_terms(), wp_count_terms(), rest_ensure_response(), ceil(), ->header(), ceil(), ->header(), ->get_query_params(), rest_get_route_for_taxonomy_items(), rest_url(), urlencode_deep(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header()

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-dev237113–178291 fewer instruction than PHP 8.5
8.5238113–17829
8.4238113–17829
8.3238113–17829
8.2238113–17829
8.1238113–17829
7.4238113–17829

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 · 1

One hook fires while WP_REST_Terms_Controller::get_items() runs, in this order:

  1. apply_filters( rest_{$this->taxonomy}_query )filterline 343 (+108 into the body)

    Filters get_terms() arguments when querying terms via the REST API.

Uses · 15

Show all 15

Source code

	public function get_items( $request ) { 		// Retrieve the list of registered collection query parameters.		$registered = $this->get_collection_params(); 		/*		 * This array defines mappings between public API query parameters whose		 * values are accepted as-passed, and their internal WP_Query parameter		 * name equivalents (some are the same). Only values which are also		 * present in $registered will be set.		 */		$parameter_mappings = array(			'exclude'    => 'exclude',			'include'    => 'include',			'order'      => 'order',			'orderby'    => 'orderby',			'post'       => 'post',			'hide_empty' => 'hide_empty',			'per_page'   => 'number',			'search'     => 'search',			'slug'       => 'slug',		); 		$prepared_args = array( 'taxonomy' => $this->taxonomy ); 		/*		 * For each known parameter which is both registered and present in the request,		 * set the parameter's value on the query $prepared_args.		 */		foreach ( $parameter_mappings as $api_param => $wp_param ) {			if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {				$prepared_args[ $wp_param ] = $request[ $api_param ];			}		} 		if ( isset( $prepared_args['orderby'] ) && isset( $request['orderby'] ) ) {			$orderby_mappings = array(				'include_slugs' => 'slug__in',			); 			if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {				$prepared_args['orderby'] = $orderby_mappings[ $request['orderby'] ];			}		} 		if ( isset( $registered['offset'] ) && ! empty( $request['offset'] ) ) {			$prepared_args['offset'] = $request['offset'];		} else {			$prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number'];		} 		$taxonomy_obj = get_taxonomy( $this->taxonomy ); 		if ( $taxonomy_obj->hierarchical && isset( $registered['parent'], $request['parent'] ) ) {			if ( 0 === $request['parent'] ) {				// Only query top-level terms.				$prepared_args['parent'] = 0;			} else {				if ( $request['parent'] ) {					$prepared_args['parent'] = $request['parent'];				}			}		} 		/*		 * When a taxonomy is registered with an 'args' array,		 * those params override the `$args` passed to this function.		 *		 * We only need to do this if no `post` argument is provided.		 * Otherwise, terms will be fetched using `wp_get_object_terms()`,		 * which respects the default query arguments set for the taxonomy.		 */		if (			empty( $prepared_args['post'] ) &&			isset( $taxonomy_obj->args ) &&			is_array( $taxonomy_obj->args )		) {			$prepared_args = array_merge( $prepared_args, $taxonomy_obj->args );		}

Changelog

Introduced in 4.7.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.

6.8.0
Respect default query arguments set for the taxonomy upon registration.from the docblock
4.7.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.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.