wp_insert_post deleting previous post custom meta

Question

I have created a custom post type called “pay stub” and two custom meta fields “net pay” and “link” (the link to the pay stub file) and two custom taxonomies, “status” and “employee”.

I set the custom fields in the admin and click save. Then someone else on the front end checks the pay stubs and clicks approved at which point, wp_set_post_terms is fired to change the “status” from “pending” to “approved” ONLY IF the “net pay” custom meta is not empty also wp_mail is fired to send emails. On the click of that approve button, wp_insert_post is also fired and creates two new pay stub posts for the next week with a “status” of “pending”(ID=46), “employee” is set relevant to the current employee, and no custom meta is set.

//Create next week's pay stub.
function create_next_pay_stub( $employee ) {
if ( !get_last_pay_stub( '', $employee, '' ) ) {
    $next_pay_stub = array(
    'post_type'    => 'pay-stub',
    'post_title'   => next_pay_period(),
    'post_status'  => 'publish',
    'post_author'  => 1,
    'tax_input'    => array(
        'status'   => 46,
        'employee' => $employee,
    ),
);
    $next_pay_stub_id = wp_insert_post( $next_pay_stub );
    return $next_pay_stub_id;
}
}

Everything works flawlessly except for the wp_insert_post function. It successfully creates new pay stubs with everything set correctly but it removes all custom meta for the latest pay stub and as a result, “net pay” becomes empty and “status” cannot be set to “approved” since “net pay” is empty. It also creates and extra pay stub with no “status” or “employee” set.

I know that wp_insert_post is the problem because if I fire the function above directly then the same problem which I just described happens.
Any ideas?

0
, , , Tristan Glaw 2 years 2019-11-22T15:08:13-05:00 0 Answers 87 views 0

Leave an answer

Browse
Browse