Take the 2-minute tour ×

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']) {
        return $post_id;
} elseif (!current_user_can('edit_page', $post_id)) {
    return $post_id;

// exit on 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');
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Yep, wp_insert_post will call save_post, hence the closed-loop.


remove_action( 'save_post', 'generate_ceu');
$pid = wp_insert_post($new_post);
add_action( 'save_post', 'generate_ceu');
share|improve this answer
Thanks that looks right from all the research I have done so far. I did find another way to implement this that I posted below. –  RiotAct Oct 25 '13 at 18:30
add comment

I found this post that helped me get a little further....so I ended up added a checkbox to the invoice so if you wanted to generate the ceu at that point, it could be done. It is not as automated as I would like, I was hoping to have it check the invoice against the balance but I will mess with it some more. This is working great so far:

function create_ceu ($post_id) {
if ( get_post_type($post_id) == 'invoice') {

    if(isset($_POST['generate_ceu'])) {

    $post_title = 'CEU'.date('YmdHs');
    $ceupost = array(
        'post_status' => 'publish', 
        'post_title' => $post_title,
        'post_type' => 'ceu', 
        'post_author' => 1);  
    wp_insert_post( $ceupost );


    else {}

   } // end check for post type
 } // end function      

add_action('save_post', 'create_ceu',10,2);
share|improve this answer
add comment

Your Answer


