I've tried substituting "cred_submit_complete" for "wp_insert_post" but it keeps throwing a critical error.
/**
* Description: Sets the invoice number the first time an auction vehicle is saved based on what the previous highest invoice number and incrementing by "1".
*/
// only create invoice number if the post in new, NOT an update
function set_invoice_number( $post_id, $post, $update ) {
if ( wp_is_post_revision( $post_id ) ) {
return;
}
if ( 'auction-vehicle' == $post->post_type ) {
if( !$update ){
$args = array(
'post_type' => 'auction-vehicle',
);
$myloop = new WP_Query( $args );
//loop through all posts of this post type to find the highest invoice number
$find_highest_id = 0;
while ( $myloop->have_posts() ) : $myloop->the_post();
$this_post_id = get_the_ID();
$invoice_number = get_post_meta( $this_post_id, 'wpcf-invoice-number', true );
if($invoice_number > $find_highest_id){
$find_highest_id = $invoice_number;
}
endwhile;
wp_reset_postdata();
//take the highest value and increment it then save as client id for the new client
$invoice_number = $find_highest_id + 1;
update_post_meta( $post_id, 'wpcf-invoice-number', $invoice_number);
}
}
}
add_action( 'wp_insert_post', 'set_invoice_number', 10, 3 );
Hello,
When you get the "critical error" message, that means there are some PHP error in your website, please follow our document to get the PHP debug logs:
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/#php-debugging
Then fix your custom PHP codes.
I took a different approach.
You can close this ticket.