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
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
- Scaling
- Constant
- Instructions
- 3
- Plugin surface
- 1 hook
- Called by
- 1
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 419.
Third-party callbacks on 'rest_{$this->post_type}_query' 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
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()one call below WP_REST_Posts_Controller::get_items() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 3 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 417 | 3 | 54 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 419 | 3 | 54 | |
| 8.4 | 419 | 3 | 54 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 422 | 3 | 54 | |
| 8.2 | 422 | 3 | 54 | Different branch profile from PHP 8.1 |
| 8.1 | 422 | 3 | 55 | 3 more instructions than PHP 7.4 |
| 7.4 | 419 | 102–196 | 55 |
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:
- 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
- __()Retrieves the translation of $text.
- get_option()Retrieves an option value based on an option name.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- add_filter()Adds a callback function to a filter hook.
- update_post_author_caches()Updates post author user caches for a list of post objects.
- update_post_parent_caches()Updates parent post caches for a list of post objects.
- post_type_supports()Checks a post type's support for a given feature.
- update_post_thumbnail_cache()Updates cache for thumbnails in the current loop.
- remove_filter()Removes a callback function from a filter hook.
- rest_ensure_response()Ensures a REST response is a response object (for consistency).
- rest_url()Retrieves the URL to a REST endpoint.
- rest_get_route_for_post_type_items()Gets the REST API route for a post type.
Show all 24
- add_query_arg()Retrieves a modified URL query string.
- urlencode_deep()Navigates through an array, object, or scalar, and encodes the values to be used in a URL.
- WP_Error::__construct()Initializes the error.
- WP_REST_Posts_Controller::get_collection_params()Retrieves the query params for the posts collection.
- WP_REST_Posts_Controller::prepare_tax_query()Prepares the 'tax_query' for a collection of posts.
- WP_REST_Posts_Controller::prepare_items_query()Determines the allowed query_vars for a get_items() response and prepares them for WP_Query.
- WP_Query::__construct()Constructor.
- WP_REST_Posts_Controller::check_update_permission()Checks if a post can be edited.
- WP_REST_Posts_Controller::check_read_permission()Checks if a post can be read.
- WP_REST_Posts_Controller::prepare_item_for_response()Prepares a single post output for response.
- WP_REST_Posts_Controller::prepare_response_for_collection()
- WP_REST_Response::__construct()
Used by · 1
- WP_REST_Font_Faces_Controller::get_items()Retrieves a collection of font faces within the parent font family.
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.
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.