_wp_translate_postdata( bool $update = false, array|null $post_data = null ): array|WP_Error
- Since
- 2.6.0
- Source
wp-admin/includes/post.php:21
$_POST data from form names to DB post columns.Description
Manipulates $_POST directly.
Compatibility
- WordPress
- since 2.6.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
$updatebooloptional- Whether the post already exists.Default:
false $post_dataarray|nulloptional- The array of post data to process.
Defaults to the$_POSTsuperglobal.Default:null
Return value
array|WP_Error- Array of post data on success, WP_Error on failure.
Performance profile
How much work a call to _wp_translate_postdata() 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
- 78–174
- Plugin surface
- None
- Called by
- 5
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 450.
Nothing here hands control to plugin code.
5 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()one call below _wp_translate_postdata() - querycontent query
get_post()one call below _wp_translate_postdata()
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 · 20 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _wp_translate_postdata() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
current_user_can() | 78 | get_post_type_object(), current_user_can(), get_current_user_id() |
current_user_can() | 89–90 | get_post_type_object(), current_user_can(), get_current_user_id(), get_taxonomy(), current_user_can() |
current_user_can() && !$aa && !$mm && !$jj | 138–142 | get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), __() |
current_user_can() && !$aa && !$mm && !$jj | 141–148 | get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate() |
current_user_can() && !$aa && !$mm && $jj | 142–145 | get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), __() |
current_user_can() && !$aa && !$mm && $jj | 145–151 | get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate() |
current_user_can() && !$aa && !$mm && !$jj | 146–153 | get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), get_post_field() |
current_user_can() && !$aa && !$mm && !$jj && $previous_date !== false | 150–154 | get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), get_gmt_from_date() |
current_user_can() && !$aa && !$mm && $jj | 150–156 | get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), get_post_field() |
current_user_can() && !$aa && !$mm && !$jj | 152–160 | get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), get_taxonomy(), current_user_can() |
current_user_can() && !$aa && !$mm && $jj && $previous_date !== false | 154–157 | get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), get_gmt_from_date() |
current_user_can() && !$aa && !$mm && !$jj && $previous_date !== false | 155–159 | get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), get_post_field(), get_gmt_from_date() |
8 further outcomes, up to 174 instructions
current_user_can() && !$aa && !$mm && $jj | 156–163 | get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), get_taxonomy(), current_user_can() |
current_user_can() && !$aa && !$mm && !$jj | 157–165 | get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), get_post_field(), get_taxonomy(), current_user_can() |
current_user_can() && !$aa && !$mm && $jj && $previous_date !== false | 159–162 | get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), get_post_field(), get_gmt_from_date() |
current_user_can() && !$aa && !$mm && !$jj && $previous_date !== false | 161–166 | get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), get_gmt_from_date(), get_taxonomy(), current_user_can() |
current_user_can() && !$aa && !$mm && $jj | 161–168 | get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), get_post_field(), get_taxonomy(), current_user_can() |
current_user_can() && !$aa && !$mm && $jj && $previous_date !== false | 165–169 | get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), get_gmt_from_date(), get_taxonomy(), current_user_can() |
current_user_can() && !$aa && !$mm && !$jj && $previous_date !== false | 166–171 | get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), get_post_field(), get_gmt_from_date(), get_taxonomy(), current_user_can() |
current_user_can() && !$aa && !$mm && $jj && $previous_date !== false | 170–174 | get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), get_post_field(), get_gmt_from_date(), get_taxonomy(), current_user_can() |
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 | 445 | 78–172 | 70 | 5 fewer instructions than PHP 8.5 |
| 8.5 | 450 | 78–174 | 70 | |
| 8.4 | 450 | 78–174 | 70 | |
| 8.3 | 450 | 78–174 | 70 | |
| 8.2 | 450 | 78–174 | 70 | |
| 8.1 | 450 | 78–174 | 70 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 453 | 78–177 | 70 |
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.
Uses · 11
- get_post_type_object()Retrieves a post type object by name.
- current_user_can()Returns whether the current user has the specified capability.
- __()Retrieves the translation of $text.
- get_current_user_id()Gets the current user's ID.
- sanitize_key()Sanitizes a string key.
- get_post_status_object()Retrieves a post status object by name.
- get_post_field()Retrieves data from a post field based on Post ID.
- wp_checkdate()Tests if the supplied date is valid for the Gregorian calendar.
- get_gmt_from_date()Given a date in the timezone of the site, returns that date in UTC.
- get_taxonomy()Retrieves the taxonomy object of $taxonomy.
- WP_Error::__construct()Initializes the error.
Used by · 5
- bulk_edit_posts()Processes the post data for the bulk editing of posts.
- edit_post()Updates an existing post with values provided in `$_POST`.
- wp_ajax_upload_attachment()Handles uploading attachments via AJAX.
- wp_create_post_autosave()Creates autosave data for the specified post from `$_POST` data.
- wp_write_post()Creates a new post from the "Write Post" form using `$_POST` information.
Source code
function _wp_translate_postdata( $update = false, $post_data = null ) { if ( empty( $post_data ) ) { $post_data = &$_POST; } if ( $update ) { $post_data['ID'] = (int) $post_data['post_ID']; } $ptype = get_post_type_object( $post_data['post_type'] ); if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) { if ( 'page' === $post_data['post_type'] ) { return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) ); } else { return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) ); } } elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) { if ( 'page' === $post_data['post_type'] ) { return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) ); } else { return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) ); } } if ( isset( $post_data['content'] ) ) { $post_data['post_content'] = $post_data['content']; } if ( isset( $post_data['excerpt'] ) ) { $post_data['post_excerpt'] = $post_data['excerpt']; } if ( isset( $post_data['parent_id'] ) ) { $post_data['post_parent'] = (int) $post_data['parent_id']; } if ( isset( $post_data['trackback_url'] ) ) { $post_data['to_ping'] = $post_data['trackback_url']; } $post_data['user_ID'] = get_current_user_id(); if ( ! empty( $post_data['post_author_override'] ) ) { $post_data['post_author'] = (int) $post_data['post_author_override']; } else { if ( ! empty( $post_data['post_author'] ) ) { $post_data['post_author'] = (int) $post_data['post_author']; } else { $post_data['post_author'] = (int) $post_data['user_ID']; } } if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] !== $post_data['user_ID'] ) && ! current_user_can( $ptype->cap->edit_others_posts ) ) { if ( $update ) { if ( 'page' === $post_data['post_type'] ) { return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) ); } else { return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) ); } } else { if ( 'page' === $post_data['post_type'] ) { return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) ); } else { return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) ); } } } if ( ! empty( $post_data['post_status'] ) ) { $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); // No longer an auto-draft. if ( 'auto-draft' === $post_data['post_status'] ) { $post_data['post_status'] = 'draft'; }Changelog
Introduced in 2.6.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-admin/includes/post.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.