How can i Insert another post on post save?

663 views Asked by At

I'm trying to insert new post on save_post hook and wp_insert_post function in Wordpress. When i try to save or update post it's triggering an infinite loop. Can anyone help?

Here is my code:

function mv_save_wc_order_other_fields( $post_id ) {    
    if(isset($_POST[ '....' ]) && !empty($_POST["...."])){
    if($_POST[ '....' ] == 3){          
        $my_post = array(
                   'post_title'    => "$post_id Bill",
                    'post_content'  => "-",
                    'post_status'   => 'publish',
                    'post_type'   => 'tahsilat',
                   'post_author'   => 1,
                 
              );
         $bill_id = wp_insert_post( $my_post, $wp_error );    
          update_post_meta( $bill_id, 'customer', $_POST[ 'user' ] );
          update_post_meta( $bill_id, 'customer', $_POST[ 'user' ] );
    }else{
        update_post_meta( $post_id, 'payment', $_POST[ '...' ] );
        update_post_meta( $post_id, 'amount', $_POST[ 'amount' ] );
    }
    
    }
   add_action( 'save_post', 'mv_save_wc_order_other_fields', 10, 1 );       
2

There are 2 answers

0
Bhautik On

You can avoid an infinite loop issue by removing your action and add after your wp_insert_post() function code. check below code.

function mv_save_wc_order_other_fields( $post_id ) {

    remove_action( 'save_post', 'mv_save_wc_order_other_fields' );

    if(isset($_POST[ '....' ]) && !empty($_POST["...."])){
        if($_POST[ '....' ] == 3){          
            $my_post = array(
                'post_title'    => "$post_id Bill",
                'post_content'  => "-",
                'post_status'   => 'publish',
                'post_type'   => 'tahsilat',
                'post_author'   => 1, 
            );
            $bill_id = wp_insert_post( $my_post, $wp_error );    
            update_post_meta( $bill_id, 'customer', $_POST[ 'user' ] );
            update_post_meta( $bill_id, 'customer', $_POST[ 'user' ] );
        }else{
            update_post_meta( $post_id, 'payment', $_POST[ '...' ] );
            update_post_meta( $post_id, 'amount', $_POST[ 'amount' ] );
        }

    add_action( 'save_post', 'mv_save_wc_order_other_fields' );

}
add_action( 'save_post', 'mv_save_wc_order_other_fields', 10, 1 ); 
0
swg1cor14 On

You want to use transition_post_status so you can trigger only if status changes. Here's what I did:

function video_post_created($new, $old, $post) {
    if ( ( $new == 'publish' ) && ( $old != 'publish' ) && ( $post->post_type == 'roku_video' ) ) {
        my_update_post_meta_function($post);
    }
}
add_action( 'transition_post_status', 'video_post_created', 10, 3 );

Notice how im checking if the new status is publish but came from a different status (such as draft) and checking for my specific post type. Mine was a custom post type. And if that all matches, then I call my own function called my_update_post_meta_function and pass the post object. You don't have to call another function. You can put your code right there instead. I was just keeping it clean.