When I save my custom post_type ‘product’, I’d like to set their status to my custom status ‘incomplete’.
1 Answer
Hook wp_insert_post_data
filter to force a post status to be set as incomplete before it can be set as published. With the following code only a post that is set as incomplete can be saved:
add_filter( 'wp_insert_post_data', 'prevent_post_change', 20, 2 );
function prevent_post_change( $data, $postarr ) {
if ( ! isset($postarr['ID']) || ! $postarr['ID'] ) return $data;
if ( $postarr['post_type'] !== 'product' ) return $data; // only for products
$old = get_post($postarr['ID']); // the post before update
if (
$old->post_status !== 'incomplete' &&
$old->post_status !== 'trash' && // without this post restoring from trash fail
$data['post_status'] === 'publish'
) {
// set post to incomplete before being published
$data['post_status'] = 'incomplete';
}
return $data;
}