wp_update_post( array|object $postarr = array(), bool $wp_error = false, bool $fire_after_hooks = true ): int|WP_Error
- Since
- 1.0.0, 3.5.0, 5.6.0
- Source
wp-includes/post.php:5147
Description
The date does not have to be set for drafts. You can set the date and it will not be overridden.
Compatibility
- WordPress
- since 5.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
$postarrarray|objectoptional- Post data. Arrays are expected to be escaped, objects are not. See wp_insert_post() for accepted arguments.
Default array.Default:array() $wp_errorbooloptional- Whether to return a WP_Error on failure. Default false.Default:
false $fire_after_hooksbooloptional- Whether to fire the after insert hooks. Default true.Default:
true
Return value
int|WP_Error- The post ID on success. The value 0 or WP_Error on failure.
Performance profile
How much work a call to wp_update_post() 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
- 17–105
- Plugin surface
- None
- Called by
- 31
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 124.
Nothing here hands control to plugin code.
31 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 - optionoption read or write
get_option()one call below wp_update_post() - cacheobject cache
wp_cache_add()one call below wp_update_post() - hookthird-party callbacks
apply_filters()one call below wp_update_post()
Further down the call graph this can also reach 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 · 28 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_update_post() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_object($postarr) && $post === null | 17 | get_post() |
!is_object($postarr) && $post === null | 24 | get_post(), __() |
is_object($postarr) && $post === null | 25 | get_object_vars(), wp_slash(), get_post() |
is_object($postarr) && $post === null | 32 | get_object_vars(), wp_slash(), get_post(), __() |
!is_object($postarr) && $post !== null | 43–60 | get_post(), wp_slash(), array_merge(), wp_insert_attachment() |
!is_object($postarr) && $post !== null && !isset($postarr) | 44–61 | get_post(), wp_slash(), array_merge(), wp_insert_post() |
!is_object($postarr) && $post !== null | 50–67 | get_post(), wp_slash(), array_merge(), current_time(), wp_insert_attachment() |
!is_object($postarr) && $post !== null && isset($postarr) && !is_object_in_taxonomy() | 51–68 | get_post(), wp_slash(), array_merge(), is_object_in_taxonomy(), wp_insert_post() |
!is_object($postarr) && $post !== null && !isset($postarr) | 51–68 | get_post(), wp_slash(), array_merge(), current_time(), wp_insert_post() |
is_object($postarr) && $post !== null | 51–68 | get_object_vars(), wp_slash(), get_post(), wp_slash(), array_merge(), wp_insert_attachment() |
is_object($postarr) && $post !== null && !isset($postarr) | 52–69 | get_object_vars(), wp_slash(), get_post(), wp_slash(), array_merge(), wp_insert_post() |
!is_object($postarr) && $post !== null && isset($postarr) && !is_object_in_taxonomy() | 58–75 | get_post(), wp_slash(), array_merge(), current_time(), is_object_in_taxonomy(), wp_insert_post() |
16 further outcomes, up to 105 instructions
is_object($postarr) && $post !== null | 58–75 | get_object_vars(), wp_slash(), get_post(), wp_slash(), array_merge(), current_time(), wp_insert_attachment() |
is_object($postarr) && $post !== null && isset($postarr) && !is_object_in_taxonomy() | 59–76 | get_object_vars(), wp_slash(), get_post(), wp_slash(), array_merge(), is_object_in_taxonomy(), wp_insert_post() |
is_object($postarr) && $post !== null && !isset($postarr) | 59–76 | get_object_vars(), wp_slash(), get_post(), wp_slash(), array_merge(), current_time(), wp_insert_post() |
!is_object($postarr) && $post !== null && isset($postarr) && is_object_in_taxonomy() | 63–81 | get_post(), wp_slash(), array_merge(), is_object_in_taxonomy(), get_the_terms(), wp_insert_post() |
is_object($postarr) && $post !== null && isset($postarr) && !is_object_in_taxonomy() | 66–83 | get_object_vars(), wp_slash(), get_post(), wp_slash(), array_merge(), current_time(), is_object_in_taxonomy(), wp_insert_post() |
!is_object($postarr) && $post !== null && isset($postarr) && is_object_in_taxonomy() && is_wp_error() | 67–85 | get_post(), wp_slash(), array_merge(), is_object_in_taxonomy(), get_the_terms(), is_wp_error(), wp_insert_post() |
!is_object($postarr) && $post !== null && isset($postarr) && is_object_in_taxonomy() | 70–88 | get_post(), wp_slash(), array_merge(), current_time(), is_object_in_taxonomy(), get_the_terms(), wp_insert_post() |
is_object($postarr) && $post !== null && isset($postarr) && is_object_in_taxonomy() | 71–89 | get_object_vars(), wp_slash(), get_post(), wp_slash(), array_merge(), is_object_in_taxonomy(), get_the_terms(), wp_insert_post() |
!is_object($postarr) && $post !== null && isset($postarr) && is_object_in_taxonomy() && !is_wp_error() | 72–90 | get_post(), wp_slash(), array_merge(), is_object_in_taxonomy(), get_the_terms(), is_wp_error(), wp_list_pluck(), wp_insert_post() |
!is_object($postarr) && $post !== null && isset($postarr) && is_object_in_taxonomy() && is_wp_error() | 74–92 | get_post(), wp_slash(), array_merge(), current_time(), is_object_in_taxonomy(), get_the_terms(), is_wp_error(), wp_insert_post() |
is_object($postarr) && $post !== null && isset($postarr) && is_object_in_taxonomy() && is_wp_error() | 75–93 | get_object_vars(), wp_slash(), get_post(), wp_slash(), array_merge(), is_object_in_taxonomy(), get_the_terms(), is_wp_error(), wp_insert_post() |
is_object($postarr) && $post !== null && isset($postarr) && is_object_in_taxonomy() | 78–96 | get_object_vars(), wp_slash(), get_post(), wp_slash(), array_merge(), current_time(), is_object_in_taxonomy(), get_the_terms(), wp_insert_post() |
!is_object($postarr) && $post !== null && isset($postarr) && is_object_in_taxonomy() && !is_wp_error() | 79–97 | get_post(), wp_slash(), array_merge(), current_time(), is_object_in_taxonomy(), get_the_terms(), is_wp_error(), wp_list_pluck(), wp_insert_post() |
is_object($postarr) && $post !== null && isset($postarr) && is_object_in_taxonomy() && !is_wp_error() | 80–98 | get_object_vars(), wp_slash(), get_post(), wp_slash(), array_merge(), is_object_in_taxonomy(), get_the_terms(), is_wp_error(), wp_list_pluck(), wp_insert_post() |
is_object($postarr) && $post !== null && isset($postarr) && is_object_in_taxonomy() && is_wp_error() | 82–100 | get_object_vars(), wp_slash(), get_post(), wp_slash(), array_merge(), current_time(), is_object_in_taxonomy(), get_the_terms(), is_wp_error(), wp_insert_post() |
is_object($postarr) && $post !== null && isset($postarr) && is_object_in_taxonomy() && !is_wp_error() | 87–105 | get_object_vars(), wp_slash(), get_post(), wp_slash(), array_merge(), current_time(), is_object_in_taxonomy(), get_the_terms(), is_wp_error(), wp_list_pluck(), wp_insert_post() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 124 instructions, 17–105 executed per call, 17 branches. The work does not change between versions.
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
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- get_post()Retrieves post data given a post ID or post object.
- __()Retrieves the translation of $text.
- current_time()Retrieves the current time based on specified type.
- wp_insert_attachment()Inserts an attachment.
- is_object_in_taxonomy()Determines if the given object type is associated with the given taxonomy.
- get_the_terms()Retrieves the terms of the taxonomy that are attached to the post.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_list_pluck()Plucks a certain field out of each object or array in an array.
- wp_insert_post()Inserts or update a post.
- WP_Error::__construct()Initializes the error.
Used by · 31
- WP_Customize_Manager::save_changeset_post()Saves the post for the loaded changeset.
- WP_Customize_Nav_Menus::save_nav_menus_created_posts()Publishes the auto-draft posts that were created for nav menu items.
- WP_Embed::shortcode()The do_shortcode() callback function.
- WP_REST_Autosaves_Controller::create_item()Creates, updates or deletes an autosave revision.
- WP_REST_Autosaves_Controller::create_post_autosave()Creates autosave for the specified post.
- WP_REST_Posts_Controller::update_item()Updates a single post.
- WP_REST_Templates_Controller::update_item()Updates a single template.
- _fix_attachment_links()Replaces hrefs of attachment anchors with up-to-date permalinks.
- _wp_personal_data_cleanup_requests()Cleans up failed and expired requests before displaying the list table.
- _wp_privacy_account_request_confirmed()Updates log when privacy request is confirmed.
- _wp_privacy_completed_request()Marks a request as completed by the admin and logs the current timestamp.
- bulk_edit_posts()Processes the post data for the bulk editing of posts.
Show all 31
- edit_post()Updates an existing post with values provided in `$_POST`.
- media_upload_form_handler()Handles form submissions for the legacy media uploader.
- wp_ajax_save_attachment()Handles updating attachment attributes via AJAX.
- wp_ajax_save_attachment_compat()Handles saving backward compatible attachment attributes via AJAX.
- wp_ajax_save_attachment_order()Handles saving the attachment order via AJAX.
- wp_ajax_send_attachment_to_editor()Handles sending an attachment to the editor via AJAX.
- wp_check_post_hierarchy_for_loops()Checks the given subset of the post hierarchy for hierarchy loops.
- wp_create_post_autosave()Creates autosave data for the specified post from `$_POST` data.
- wp_generate_user_request_key()Returns a confirmation key for a user action and stores the hashed version for future comparison.
- wp_restore_post_revision()Restores a post to the specified revision.
- wp_set_unique_slug_on_create_template_part()Sets a custom slug when creating auto-draft template parts.
- wp_trash_post()Moves a post or page to the Trash
- wp_untrash_post()Restores a post from the Trash.
- wp_update_custom_css_post()Updates the `custom_css` post for a given theme.
- wp_update_nav_menu_item()Saves the properties of a menu item or create a new one.
- wp_xmlrpc_server::_insert_post()Helper method for wp_newPost() and wp_editPost(), containing shared logic.
- wp_xmlrpc_server::blogger_editPost()Edits a post.
- wp_xmlrpc_server::mt_publishPost()Sets a post's publish status to 'publish'.
- wp_xmlrpc_server::mw_editPost()Edits a post.
Source code
function wp_update_post( $postarr = array(), $wp_error = false, $fire_after_hooks = true ) { if ( is_object( $postarr ) ) { // Non-escaped post was passed. $postarr = get_object_vars( $postarr ); $postarr = wp_slash( $postarr ); } // First, get all of the original fields. $post = get_post( $postarr['ID'], ARRAY_A ); if ( is_null( $post ) ) { if ( $wp_error ) { return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); } return 0; } // Escape data pulled from DB. $post = wp_slash( $post ); // Passed post category list overwrites existing category list if not empty. if ( isset( $postarr['post_category'] ) && is_array( $postarr['post_category'] ) && count( $postarr['post_category'] ) > 0 ) { $post_cats = $postarr['post_category']; } else { $post_cats = $post['post_category']; } // Drafts shouldn't be assigned a date unless explicitly done so by the user. if ( isset( $post['post_status'] ) && in_array( $post['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) && empty( $postarr['edit_date'] ) && ( '0000-00-00 00:00:00' === $post['post_date_gmt'] ) ) { $clear_date = true; } else { $clear_date = false; } // Merge old and new fields with new fields overwriting old ones. $postarr = array_merge( $post, $postarr ); $postarr['post_category'] = $post_cats; if ( $clear_date ) { $postarr['post_date'] = current_time( 'mysql' ); $postarr['post_date_gmt'] = ''; } if ( 'attachment' === $postarr['post_type'] ) { return wp_insert_attachment( $postarr, false, 0, $wp_error ); } // Discard 'tags_input' parameter if it's the same as existing post tags. if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $postarr['post_type'], 'post_tag' ) ) { $tags = get_the_terms( $postarr['ID'], 'post_tag' ); $tag_names = array(); if ( $tags && ! is_wp_error( $tags ) ) { $tag_names = wp_list_pluck( $tags, 'name' ); } if ( $postarr['tags_input'] === $tag_names ) { unset( $postarr['tags_input'] ); } } return wp_insert_post( $postarr, $wp_error, $fire_after_hooks );}Changelog
Introduced in 1.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$fire_after_hooks parameter.from the docblock$wp_error parameter to allow a WP_Error to be returned on failure.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-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.