wppaste
WordPress

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

Since
4.7.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:215
Retrieves a collection of posts.

Compatibility

WordPress
since 4.7.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_Posts_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
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
3

Executed per call on PHP 8.5. The body compiles to 419.

Plugin surface
1 hook

Third-party callbacks on 'rest_{$this->post_type}_query' 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

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()one call below WP_REST_Posts_Controller::get_items()
  • serializeserialisationmaybe_unserialize()one call below WP_REST_Posts_Controller::get_items()

Further down the call graph this can also reach query 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 · 1 distinct outcome

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

WhenInstructionsCalls it makes
always3none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev4173542 fewer instructions than PHP 8.5
8.5419354
8.44193543 fewer instructions than PHP 8.3
8.3422354
8.2422354Different branch profile from PHP 8.1
8.14223553 more instructions than PHP 7.4
7.4419102–19655

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_Posts_Controller::get_items() runs, in this order:

  1. apply_filters( rest_{$this->post_type}_query )filterline 444 (+229 into the body)

    Filters WP_Query arguments when querying posts via the REST API.

Uses · 24

Show all 24

Used by · 1

Source code

	public function get_items( $request ) { 		// Ensure a search string is set in case the orderby is set to 'relevance'.		if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) {			return new WP_Error(				'rest_no_search_term_defined',				__( 'You need to define a search term to order by relevance.' ),				array( 'status' => 400 )			);		} 		// Ensure an include parameter is set in case the orderby is set to 'include'.		if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) {			return new WP_Error(				'rest_orderby_include_missing_include',				__( 'You need to define an include parameter to order by include.' ),				array( 'status' => 400 )			);		} 		// Retrieve the list of registered collection query parameters.		$registered = $this->get_collection_params();		$args       = array(); 		/*		 * 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(			'author'         => 'author__in',			'author_exclude' => 'author__not_in',			'exclude'        => 'post__not_in',			'include'        => 'post__in',			'ignore_sticky'  => 'ignore_sticky_posts',			'menu_order'     => 'menu_order',			'offset'         => 'offset',			'order'          => 'order',			'orderby'        => 'orderby',			'page'           => 'paged',			'parent'         => 'post_parent__in',			'parent_exclude' => 'post_parent__not_in',			'search'         => 's',			'search_columns' => 'search_columns',			'slug'           => 'post_name__in',			'status'         => 'post_status',		); 		/*		 * For each known parameter which is both registered and present in the request,		 * set the parameter's value on the query $args.		 */		foreach ( $parameter_mappings as $api_param => $wp_param ) {			if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {				$args[ $wp_param ] = $request[ $api_param ];			}		} 		// Check for & assign any parameters which require special handling or setting.		$args['date_query'] = array(); 		if ( isset( $registered['before'], $request['before'] ) ) {			$args['date_query'][] = array(				'before' => $request['before'],				'column' => 'post_date',			);		} 		if ( isset( $registered['modified_before'], $request['modified_before'] ) ) {			$args['date_query'][] = array(				'before' => $request['modified_before'],				'column' => 'post_modified',			);		} 		if ( isset( $registered['after'], $request['after'] ) ) {			$args['date_query'][] = array(				'after'  => $request['after'],				'column' => 'post_date',

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.

About this page

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