wp_insert_post deleting previous post custom meta

SOLVED… for now

I think this code was my problem:

// use reset postdata to restore orginal query
    wp_reset_postdata();

    if ( $current_slug == $approve_slug && $success === 1 && isset ($success)  ) {
        $next_pay_stub_id = create_next_pay_stub( $term->term_id );
    }
} //this is the closing bracket of my foreach statement

which I changed to

if ( $current_slug == $approve_slug && $success === 1 && isset ($success)  ) {
        $next_pay_stub_id = create_next_pay_stub( $term->term_id );
    }
} //this is the closing bracket of my foreach statement
    // use reset postdata to restore orginal query
    wp_reset_postdata();

So I think since the create_next_pay_stub(); function was after wp_reset_postdata();, it was not in the loop, but sort of was…? When I put reset postdata after the closing bracket for the foreach statement it works, and makes sense why it works now and not before.

I’m not a WP or PHP expert whatsoever, I compiled this entire pay stub functionality by combining many tutorials and google searches together.

Thanks to WebElaine for the helping comments!