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:Update:
instead of
wp_insert_post()
usewp_update_post
like this:this way you only need to specify the fields that were updated and never worry about stuff like what was the original post type.
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.