WP_REST_Posts_Controller::prepare_item_for_response( WP_Post $item, WP_REST_Request $request ): WP_REST_Response
- Since
- 4.7.0, 5.9.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:1823
Compatibility
- WordPress
- since 5.9.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
$itemWP_Post- Post object.
$requestWP_REST_Request- Request object.
Return value
WP_REST_Response- Response object.
Performance profile
How much work a call to WP_REST_Posts_Controller::prepare_item_for_response() 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
- Constant
- Instructions
- 2
- Plugin surface
- 5 hooks
- Called by
- 6
Reaches the database via get_post().
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 656.
Third-party callbacks on 'get_the_guid', 'the_content', 'get_the_excerpt' run inside this call, and their cost is not bounded by anything here.
6 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()called directly - cacheobject cache
wp_cache_get()one call below WP_REST_Posts_Controller::prepare_item_for_response() - serializeserialisation
maybe_unserialize()one call below WP_REST_Posts_Controller::prepare_item_for_response() - querycontent query
get_post()one call below WP_REST_Posts_Controller::prepare_item_for_response()
Further down the call graph this can also reach transient. That is 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::prepare_item_for_response() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 2 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 654 | 2 | 59 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 656 | 2 | 59 | |
| 8.4 | 656 | 2 | 59 | |
| 8.3 | 656 | 2 | 59 | |
| 8.2 | 656 | 2 | 59 | |
| 8.1 | 656 | 2 | 59 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 658 | 231–305 | 59 |
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 · 5
5 hooks fire while WP_REST_Posts_Controller::prepare_item_for_response() runs, in this order:
- apply_filters( get_the_guid )filterline 1862 (+39 into the body)
Filters the Global Unique Identifier (guid) of the post.
- apply_filters( get_the_excerpt )filterline 1963 (+140 into the body)
Filters the retrieved post excerpt.
- apply_filters( rest_prepare_{$this->post_type} )filterline 2114 (+291 into the body)
Filters the post data for a REST API response.
Uses · 31
- setup_postdata()Set up global post data.
- rest_is_field_included()Given an array of fields to include in a response, some of which may be `nested.fields`, determine whether the provided field should be included in the response body.
- get_gmt_from_date()Given a date in the timezone of the site, returns that date in UTC.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_option()Retrieves an option value based on an option name.
- get_permalink()Retrieves the full permalink for the current post or post ID.
- add_filter()Adds a callback function to a filter hook.
- get_the_title()Retrieves the post title.
- remove_filter()Removes a callback function from a filter hook.
- post_password_required()Determines whether the post requires password and whether a correct password has been provided.
- block_version()Returns the current version of the block format that the content string is using.
- get_post_thumbnail_id()Retrieves the post thumbnail ID.
Show all 31
- is_sticky()Determines whether a post is sticky.
- get_page_template_slug()Gets the specific template filename for a given post.
- get_post_format()Retrieve the format slug for a post
- wp_list_filter()Filters a list of objects, based on a set of key => value arguments.
- get_object_taxonomies()Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name.
- get_the_terms()Retrieves the terms of the taxonomy that are attached to the post.
- wp_list_pluck()Plucks a certain field out of each object or array in an array.
- get_post_type_object()Retrieves a post type object by name.
- is_post_type_viewable()Determines whether a post type is considered "viewable".
- get_sample_permalink()Returns a sample permalink based on the post name.
- get_post_class()Retrieves an array of the class names for the post container element.
- rest_ensure_response()Ensures a REST response is a response object (for consistency).
- WP_REST_Posts_Controller::get_fields_for_response()
- WP_REST_Posts_Controller::prepare_date_response()Checks the post_date_gmt or modified_gmt and prepare any post or modified date for single post output.
- WP_REST_Posts_Controller::can_access_password_content()Checks if the user can access password-protected content.
- WP_REST_Posts_Controller::add_additional_fields_to_object()
- WP_REST_Posts_Controller::filter_response_by_context()
- WP_REST_Posts_Controller::prepare_links()Prepares links for the request.
- WP_REST_Posts_Controller::get_available_actions()Gets the link relations available for the post and current user.
Used by · 6
- WP_REST_Attachments_Controller::prepare_item_for_response()Prepares a single attachment output for response.
- WP_REST_Posts_Controller::create_item()Creates a single post.
- WP_REST_Posts_Controller::delete_item()Deletes a single post.
- WP_REST_Posts_Controller::get_item()Retrieves a single post.
- WP_REST_Posts_Controller::get_items()Retrieves a collection of posts.
- WP_REST_Posts_Controller::update_item()Updates a single post.
Source code
public function prepare_item_for_response( $item, $request ) { // Restores the more descriptive, specific name for use within this method. $post = $item; $GLOBALS['post'] = $post; setup_postdata( $post ); $fields = $this->get_fields_for_response( $request ); // Base fields for every post. $data = array(); if ( rest_is_field_included( 'id', $fields ) ) { $data['id'] = $post->ID; } if ( rest_is_field_included( 'date', $fields ) ) { $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date ); } if ( rest_is_field_included( 'date_gmt', $fields ) ) { /* * For drafts, `post_date_gmt` may not be set, indicating that the date * of the draft should be updated each time it is saved (see #38883). * In this case, shim the value based on the `post_date` field * with the site's timezone offset applied. */ if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) { $post_date_gmt = get_gmt_from_date( $post->post_date ); } else { $post_date_gmt = $post->post_date_gmt; } $data['date_gmt'] = $this->prepare_date_response( $post_date_gmt ); } if ( rest_is_field_included( 'guid', $fields ) ) { $data['guid'] = array( /** This filter is documented in wp-includes/post-template.php */ 'rendered' => apply_filters( 'get_the_guid', $post->guid, $post->ID ), 'raw' => $post->guid, ); } if ( rest_is_field_included( 'modified', $fields ) ) { $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ); } if ( rest_is_field_included( 'modified_gmt', $fields ) ) { /* * For drafts, `post_modified_gmt` may not be set (see `post_date_gmt` comments * above). In this case, shim the value based on the `post_modified` field * with the site's timezone offset applied. */ if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) { $post_modified_gmt = gmdate( 'Y-m-d H:i:s', strtotime( $post->post_modified ) - (int) ( (float) get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ); } else { $post_modified_gmt = $post->post_modified_gmt; } $data['modified_gmt'] = $this->prepare_date_response( $post_modified_gmt ); } if ( rest_is_field_included( 'password', $fields ) ) { $data['password'] = $post->post_password; } if ( rest_is_field_included( 'slug', $fields ) ) { $data['slug'] = $post->post_name; } if ( rest_is_field_included( 'status', $fields ) ) { $data['status'] = $post->post_status; } if ( rest_is_field_included( 'type', $fields ) ) { $data['type'] = $post->post_type; } if ( rest_is_field_included( 'link', $fields ) ) { $data['link'] = get_permalink( $post->ID );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.
$post to $item to match parent class for PHP 8 named parameter support.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.