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!
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.