WordPress.org

Ready to get started?Download WordPress

Codex

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

Function Reference/wp update post

Contents

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 ); ?> 

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. Filling out the ID field is not strictly necessary but without it there is little point to using the function.
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_content' => 'This is the updated content.'
  );

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

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');
?>

Caution - Updating the post_name/slug

Updating the post_name field (or slug) via the wp_update_post() function might not work in many cases (Cf. the core code if you want more details). To update the slug, you should call the wp_insert_post_data filter to modify the $data['post_name'] value before it gets inserted into the database.

Here is a code sample that works around this problem :

add_filter('wp_insert_post_data', 'update_post_slug'); // We need this to update the slug, otherwise "wp_update_post" overwrites it
wp_update_post(// We update the french post
        array (
          'ID'            => $project_id, 
          'post_title'    => $_POST[$field_name_buffer],
));
remove_filter('wp_insert_post_data', array(&$this, 'update_post_slug'));

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.