wppaste
WordPress

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:5231
Updates a post with new post data.

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

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
17–105

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 124.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
31

31 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • optionoption read or writeget_option()one call below wp_update_post()
  • cacheobject cachewp_cache_add()one call below wp_update_post()
  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
!is_object($postarr) && $post === null17get_post()
!is_object($postarr) && $post === null24get_post(), __()
is_object($postarr) && $post === null25get_object_vars(), wp_slash(), get_post()
is_object($postarr) && $post === null32get_object_vars(), wp_slash(), get_post(), __()
!is_object($postarr) && $post !== null43–60get_post(), wp_slash(), array_merge(), wp_insert_attachment()
!is_object($postarr) && $post !== null && !isset($postarr)44–61get_post(), wp_slash(), array_merge(), wp_insert_post()
!is_object($postarr) && $post !== null50–67get_post(), wp_slash(), array_merge(), current_time(), wp_insert_attachment()
!is_object($postarr) && $post !== null && isset($postarr) && !is_object_in_taxonomy()51–68get_post(), wp_slash(), array_merge(), is_object_in_taxonomy(), wp_insert_post()
!is_object($postarr) && $post !== null && !isset($postarr)51–68get_post(), wp_slash(), array_merge(), current_time(), wp_insert_post()
is_object($postarr) && $post !== null51–68get_object_vars(), wp_slash(), get_post(), wp_slash(), array_merge(), wp_insert_attachment()
is_object($postarr) && $post !== null && !isset($postarr)52–69get_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–75get_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 !== null58–75get_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–76get_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–76get_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–81get_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–83get_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–85get_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–88get_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–89get_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–90get_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–92get_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–93get_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–96get_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–97get_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–98get_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–100get_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–105get_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

Used by · 31

Show all 31

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.6.0
Added the $fire_after_hooks parameter.from the docblock
3.5.0
Added the $wp_error parameter to allow a WP_Error to be returned on failure.from the docblock
1.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 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.