I have a custom function that works with my custom post type. While porocessing save_post
action:
add_action( 'save_post', 'my_custom_function' );
I would like to set post status as draft (in case of a problem with getting custom data from outside api).
In my my_custom_function
function I have this little block:
if ($error == true) {
$override_post = array();
$override_post['ID'] = $post_id;
$override_post['post_status'] = 'draft';
wp_update_post( $override_post );
}
but it seems, that after save_post
is being processed, then post_status
is being set again.
Anybody have an idea, where should I hook into, so while saving post data I can modify its post_status
, post_date
and some other post data informations so they are not being overriten?
You should hook it to
wp_insert_post_data
. Then you could use a function like this to set your post status to draft:I had to make a post type with only one post_status option, and it seem to fit your needs too, as it is works exactly with the
save_post
hook.The original solution found here:
https://wp-kama.ru/function/wp_update_post