This is kind of a unique situation. I have an invoicing custom post type that when it is marked as "paid" I want to then generate another custom post type. I have a custom meta file that handles the invoice meta and am trying to get this function to work. Currently, it does work, but it creates an infinite number of new posts all with the same title and such. I have to quickly stop the page and it already has close to 1000 posts. Here is my current function, hopefully someone will see something I am doing wrong. I did try adding the post as a future post as I read somewhere that would eliminate the loop...didn't work.
// Generate CEU if payment is entered manually
function generate_ceu( $post_id ){
// check nonce
if (!isset($_POST['invoice_meta_box_nonce']) || !wp_verify_nonce($_POST['invoice_meta_box_nonce'], 'invoice_meta_box')) {
return $post_id;
}
// check capabilities
if ('invoice' == $_POST['post_type']) {
if (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
// exit on autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// Check if the payment amount already exists so we don't create another CEU
if (!get_post_meta($post->ID, 'invoice_payment_amount', true)) {
if (isset($_POST['invoice_payment_amount'])) {
$title = 'CEU'.date('YmdHs');
// Create the post
$new_post = array(
'post_title' => $title,
'post_date_gmt' => strtotime('+5 minutes'), // Set the post publish time to 5 minutes from now so it wont loop
'post_status' => 'future', // Choose: publish, preview, future, draft, etc.
'post_type' => 'ceu', //'post',page' or use a custom post type if you want to
);
$pid = wp_insert_post($new_post);
} // end check for post entry
} // end check for meta data
} // end function
add_action( 'save_post', 'generate_ceu');