WP_REST_Posts_Controller::prepare_item_for_database( WP_REST_Request $request ): stdClass|WP_Error
- Since
- 4.7.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:1257
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- Request object.
Return value
stdClass|WP_Error- Post object or WP_Error.
Performance profile
How much work a call to WP_REST_Posts_Controller::prepare_item_for_database() 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
- 80–143
- Plugin surface
- 1 hook
- Called by
- 3
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, depending on the branch taken. The body compiles to 395.
Third-party callbacks on 'rest_pre_insert_{$this->post_type}' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()one call below WP_REST_Posts_Controller::prepare_item_for_database()
Further down the call graph this can also reach 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 · 10 distinct outcomes
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_database() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($request) && !empty($request) && !empty($prepared_post) && post_password_required() | 80–93 | ->get_item_schema(), get_post_type(), get_post_type_object(), post_password_required(), __() |
!isset($request) && empty($value) && !empty($request) | 84–128 | ->get_item_schema(), get_post_type(), get_post_type_object(), apply_filters() |
!empty($request) && empty($parent) | 87–104 | ->get_item_schema(), get_post_type(), get_post_type_object(), get_post(), __() |
!isset($request) && !empty($request) && !empty($prepared_post) && !post_password_required() | 94–134 | ->get_item_schema(), get_post_type(), get_post_type_object(), post_password_required(), apply_filters() |
!empty($request) && !empty($prepared_post) && !post_password_required() && empty($parent) | 97–110 | ->get_item_schema(), get_post_type(), get_post_type_object(), post_password_required(), get_post(), __() |
!empty($request) && !empty($parent) | 102–137 | ->get_item_schema(), get_post_type(), get_post_type_object(), get_post(), apply_filters() |
!empty($request) && !empty($prepared_post) && !is_sticky() | 103–132 | ->get_item_schema(), get_post_type(), get_post_type_object(), is_sticky(), apply_filters() |
!empty($request) && !empty($prepared_post) && !is_sticky() && empty($parent) | 106–108 | ->get_item_schema(), get_post_type(), get_post_type_object(), is_sticky(), get_post(), __() |
!empty($request) && !empty($prepared_post) && !post_password_required() && !empty($parent) | 112–143 | ->get_item_schema(), get_post_type(), get_post_type_object(), post_password_required(), get_post(), apply_filters() |
!empty($request) && !empty($prepared_post) && !is_sticky() && !empty($parent) | 121–141 | ->get_item_schema(), get_post_type(), get_post_type_object(), is_sticky(), get_post(), apply_filters() |
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 | 395 | 80–143 | 64 | |
| 8.5 | 395 | 80–143 | 64 | |
| 8.4 | 395 | 80–143 | 64 | |
| 8.3 | 395 | 80–143 | 64 | |
| 8.2 | 395 | 80–143 | 64 | |
| 8.1 | 395 | 80–143 | 64 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 397 | 80–143 | 64 |
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::prepare_item_for_database() runs, in this order:
- apply_filters( rest_pre_insert_{$this->post_type} )filterline 1471 (+214 into the body)
Filters a post before it is inserted via the REST API.
Uses · 16
- is_wp_error()Checks whether the given variable is a WordPress Error.
- get_post_type()Retrieves the post type of the current post or of a given post.
- get_post_type_object()Retrieves a post type object by name.
- get_post()Retrieves post data given a post ID or post object.
- rest_get_date_with_gmt()Parses a date into both its local and UTC equivalent, in MySQL datetime format.
- get_current_user_id()Gets the current user's ID.
- get_userdata()Retrieves user info by user ID.
- __()Retrieves the translation of $text.
- is_sticky()Determines whether a post is sticky.
- post_password_required()Determines whether the post requires password and whether a correct password has been provided.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- stdClass::__construct()
Show all 16
- WP_REST_Posts_Controller::get_post()Gets the post, if the ID is valid.
- WP_REST_Posts_Controller::get_item_schema()Retrieves the post's schema, conforming to JSON Schema.
- WP_REST_Posts_Controller::handle_status_param()Determines validity and normalizes the given status parameter.
- WP_Error::__construct()Initializes the error.
Used by · 3
- WP_REST_Attachments_Controller::prepare_item_for_database()Prepares a single attachment for create or update.
- WP_REST_Posts_Controller::create_item()Creates a single post.
- WP_REST_Posts_Controller::update_item()Updates a single post.
Source code
protected function prepare_item_for_database( $request ) { $prepared_post = new stdClass(); $current_status = ''; // Post ID. if ( isset( $request['id'] ) ) { $existing_post = $this->get_post( $request['id'] ); if ( is_wp_error( $existing_post ) ) { return $existing_post; } $prepared_post->ID = $existing_post->ID; $current_status = $existing_post->post_status; } $schema = $this->get_item_schema(); // Post title. if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) { if ( is_string( $request['title'] ) ) { $prepared_post->post_title = $request['title']; } elseif ( ! empty( $request['title']['raw'] ) ) { $prepared_post->post_title = $request['title']['raw']; } } // Post content. if ( ! empty( $schema['properties']['content'] ) && isset( $request['content'] ) ) { if ( is_string( $request['content'] ) ) { $prepared_post->post_content = $request['content']; } elseif ( isset( $request['content']['raw'] ) ) { $prepared_post->post_content = $request['content']['raw']; } } // Post excerpt. if ( ! empty( $schema['properties']['excerpt'] ) && isset( $request['excerpt'] ) ) { if ( is_string( $request['excerpt'] ) ) { $prepared_post->post_excerpt = $request['excerpt']; } elseif ( isset( $request['excerpt']['raw'] ) ) { $prepared_post->post_excerpt = $request['excerpt']['raw']; } } // Post type. if ( empty( $request['id'] ) ) { // Creating new post, use default type for the controller. $prepared_post->post_type = $this->post_type; } else { // Updating a post, use previous type. $prepared_post->post_type = get_post_type( $request['id'] ); } $post_type = get_post_type_object( $prepared_post->post_type ); // Post status. if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) && ( ! $current_status || $current_status !== $request['status'] ) ) { $status = $this->handle_status_param( $request['status'], $post_type ); if ( is_wp_error( $status ) ) { return $status; } $prepared_post->post_status = $status; } // Post date. if ( ! empty( $schema['properties']['date'] ) && ! empty( $request['date'] ) ) { $current_date = isset( $prepared_post->ID ) ? get_post( $prepared_post->ID )->post_date : false; $date_data = rest_get_date_with_gmt( $request['date'] ); if ( ! empty( $date_data ) && $current_date !== $date_data[0] ) { list( $prepared_post->post_date, $prepared_post->post_date_gmt ) = $date_data; $prepared_post->edit_date = true; } } elseif ( ! empty( $schema['properties']['date_gmt'] ) && ! empty( $request['date_gmt'] ) ) {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 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.