Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

you can use pre_post_update action hook like so:

add_action('pre_post_update','post_updating_callback');
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 ) 
      return;
    if ($post->post_status == "publish"){
        //do update stuff here.
    }
}

Update:

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.

share|improve this answer
    
+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 '11 at 0:33
    
... well usually more then one ;D –  Michal Mau Jun 1 '11 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. –  mattrepublic Jun 1 '11 at 17:55
    
I updated my answer with an example –  Bainternet Jun 1 '11 at 20:27
add comment

These two might be your friends:

add_action('edit_post','your_action');

add_action('pre_post_update','your_action');

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.

share|improve this answer
    
wp_update_post calls wp_insert_post –  Bainternet Jun 1 '11 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 '11 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 '11 at 0:58
    
Good to know then. Thanks for the micro Q&A! –  Michal Mau Jun 1 '11 at 1:00
    
Thanks for the clarification! –  mattrepublic Jun 1 '11 at 17:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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