Pippin and Jean-Baptiste Jung have very good tutorials on how to programmatically create content when a new post is published using these four actions hooks...

  • publish{post_type}
  • add_action('new_to_publish_{post_type},
  • add_action('draft_to_publish_{post_type},
  • add_action('pending_to_publish_{post_type}

... to run this script...

global $user_ID;
$new_post = array(
    'post_title' => 'My New Post',
    'post_content' => 'Lorem ipsum dolor sit amet...',
    'post_status' => 'publish',
    'post_date' => date('Y-m-d H:i:s'),
    'post_author' => $user_ID,
    'post_type' => 'post',
    'post_category' => array(0)
$post_id = wp_insert_post($new_post);

What hook should I be using to do this function only when a post is updated? Thank you!

2 Answers 2


you can use pre_post_update action hook like so:

function post_updating_callback($post_id){
    global $post;
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
    if ($post->post_status == "publish"){
        //do update stuff here.


instead of wp_insert_post() use wp_update_post like this:

//first get the original post
$postarr = get_post($post_id,'ARRAY_A');

//then set the fields you want to update
$postarr['post_title'] = "new title";
$postarr['post_content'] = "new edited content";

$post_id = wp_update_post($postarr);

this way you only need to specify the fields that were updated and never worry about stuff like what was the original post type.

  • +1 for mentioning these two important if-checks! I like how you often make this one step further in your answers! :)
    – Michal Mau
    Jun 1, 2011 at 0:33
  • ... well usually more then one ;D
    – Michal Mau
    Jun 1, 2011 at 0:34
  • Thanks @Bainternet! If the new post code is dropped in as is the new post that is programmatically it inherits the post type of the editing post, even if it is specified in the array. Do you have any suggestions? For example the code snippet above creates a new post post instead of a page.
    – MTT
    Jun 1, 2011 at 17:55
  • I updated my answer with an example
    – Bainternet
    Jun 1, 2011 at 20:27

These two might be your friends:



See: Action Reference: Post, Page, Attachment, and Category Actions for more inspiration...

PS. It depends on context but consider also using wp_update_post instead wp_insert_post.

  • wp_update_post calls wp_insert_post
    – Bainternet
    Jun 1, 2011 at 0:30
  • @Bainternet but this could be an advantage sometimes: "Unlike wp_insert_post(), it is only necessary to pass the ID of the post to be updated and the elements to be updated." What do you think?
    – Michal Mau
    Jun 1, 2011 at 0:42
  • 3
    They do the same at the end but wp_insert_post has more filters and action hooks which makes it more flexible
    – Bainternet
    Jun 1, 2011 at 0:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.