WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp update post

Description

This function updates posts (and pages) in the database. To work as expected, it is necessary to pass the ID of the post to be updated.

Note that when the post is "updated", the existing Post record is duplicated for audit/revision purposes. The primary record is then updated with the new values. Category associations, custom fields, post meta, and other related entries continue to be linked to the primary Post record.

Usage

 <?php wp_update_post$post$wp_error ); ?> 

Parameters

$post
(array/object) (optional) An array or object representing the elements that make up a post. There is a one-to-one relationship between these elements and the names of columns in the wp_posts table in the database. You must include "ID", otherwise a WP_Error will be thrown.
Default: An empty array
$wp_error
(Boolean) (optional) A Boolean that can be passed to control what is returned on failure. The default setting (false) will return a 0 if the post fails to update. However, if this is set to true, it will return with a WP_Error object.
Default: false

Example

Before calling wp_update_post() it is necessary to create an array to pass the necessary elements. Unlike wp_insert_post(), it is only necessary to pass the ID of the post to be updated and the elements to be updated. The names of the elements should match those in the database.

// Update post 37
  $my_post = array(
      'ID'           => 37,
      'post_title'   => 'This is the post title.',
      'post_content' => 'This is the updated content.',
  );

// Update the post into the database
  wp_update_post( $my_post );

Processing $wp_error

If your updates are not working, there could be an error. It is a good idea to set $wp_error to true and display the error immediately after.

<?php
// Of course, this should be done in an development environment only and commented out or removed after deploying to your production site.

$post_id = wp_update_post( $current_item, true );						  
if (is_wp_error($post_id)) {
	$errors = $post_id->get_error_messages();
	foreach ($errors as $error) {
		echo $error;
	}
}
?>

Categories

Categories need to be passed as an array of integers that match the category IDs in the database. This is the case even where only one category is assigned to the post.

Caution - Infinite loop

When executed by an action hooked into save_post (e.g. a custom metabox), wp_update_post() has the potential to create an infinite loop. This happens because (1) wp_update_post() results in save_post being fired and (2) save_post is called twice when revisions are enabled (first when creating the revision, then when updating the original post—resulting in the creation of endless revisions).

If you must update a post from code called by save_post, make sure to verify the post_type is not set to 'revision' and that the $post object does indeed need to be updated.

Likewise, an action hooked into edit_attachment can cause an infinite loop if it contains a function call to wp_update_post passing an array parameter with a key value of "ID" and an associated value that corresponds to an Attachment.

Note you will need to remove then add the hook, code sample modified from the API/Action reference: save_post

<?php
function my_function( $post_id ){
	if ( ! wp_is_post_revision( $post_id ) ){
	
		// unhook this function so it doesn't loop infinitely
		remove_action('save_post', 'my_function');
	
		// update the post, which calls save_post again
		wp_update_post( $my_args );

		// re-hook this function
		add_action('save_post', 'my_function');
	}
}
add_action('save_post', 'my_function');
?>

Scheduling posts

If you are trying to use wp_update_post() to schedule an existing draft, it will not work unless you pass $my_post->edit_date = true. WordPress will ignore the post_date when updating drafts unless edit_date is true.

Return

(integer) 
The ID of the post if the post is successfully updated in the database. Otherwise returns 0.

Change Log

Source File

wp_update_post() is located in wp-includes/post.php.

Related

wp_insert_post()

See also index of Function Reference and index of Template Tags.
This article is marked as in need of editing. You can help Codex by editing it.