0

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 (last week's) 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 an 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?

EDIT 1 (in response to WebElaine's comment):

It's hooked into an if/else statement for wp_error which is checking for errors when wp_set_post_terms is fired. If error (terms were not changed to "approved") then echo an error message, else create_next_pay_stub() (and some other actions like sending notification emails etc.)

if ( is_wp_error( $term_taxonomy_ids ) || empty( $pay_amount ) ) { // There was an error somewhere and the terms couldn't be set. ?>
<span class="alert-danger pay-stub-alert loop-pay-stub-alert">ERROR!</span>
<?php $success = 0; }
else { // Success! The terms were set. echo 'APPROVED!';
$success = 1;
send_email_on_approval ( $employee_data->user_email, $employee_data->first_name, $title, $pay_amount );
}

EDIT 2 (narrowed down the culprit):

I have been doing extensive testing and the problem arise only when this function is run in the loop of the paystubs:

function publish_announcement_on_approval () {
    $announcement_id = get_last_announcement( '' );
    $announcement_publish = array(
      'ID'           => $announcement_id,
      'post_status'   => 'publish',
  );
    wp_update_post( $announcement_publish );
}

(an announcenment is just a CPT from the WP HR Manager plugin)

The publish_announcement_on_approval () function is being called in the above wp_error function above, just after send_email_on_approval.

I originally had publish_announcement_on_approval () included in the send_email_on_approval function. But now i seperated them and it helped me realize that create_next_pay_stub is not the culprit.

I am currently testing with having publish_announcement_on_approval () out of the paystub loop. For now the function doesn't work outside of the loop, but it may be a coding error on my part. So I am currently looking into that, will report my findings.

Thanks

EDIT 3 (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!

  • Maybe I am missing something in your process, but it looks as though your wp_insert_post() function is creating a brand-new published pay-stub CPT. If so, it makes sense that there's no postmeta, as postmeta is only associated to a single post, and this is a new post. When you call wp_insert_post() you can pass in the required postmeta: wordpress.stackexchange.com/questions/218131/… – WebElaine Nov 22 '19 at 22:01
  • The next weeks pay stubs are not supposed to contain any custom meta data because that will be entered manually. So, what I meant is that when it creates next week's pay stub CPT, it clears the custom meta data for the last pay stub CPT which contains custom meta data. So basically when creating next week's pay stub it clears meta data for last week's pay stub which is already approved and should not be edited/changed anymore. – Tristan Glaw Nov 23 '19 at 13:44
  • Your function as-is wouldn't affect an existing post, so you may need to look at the surrounding code to find out what is changing the existing post. What hook are you running the create_next_pay_stub() function on? – WebElaine Dec 2 '19 at 17:54
  • Can you please update your question instead of adding code as comments, so it's a little easier to follow? – WebElaine Dec 3 '19 at 21:45
  • I edited my question. Sorry for that. Please look at EDIT 2 in my question – Tristan Glaw Dec 9 '19 at 8:12
0

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!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.