问题
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?
回答1:
You should hook it to wp_insert_post_data. Then you could use a function like this to set your post status to draft:
add_filter( 'wp_insert_post_data', 'set_post_to_draft', 99, 2 );
function set_post_to_draft( $data, $postarr ) {
if ( your_condition ) {
$data['post_status'] = 'draft';
}
return $data;
}
来源:https://stackoverflow.com/questions/15784061/wordpress-set-post-status-as-draft-in-save-post-action