wordpress set post_status as “draft” in 'save_post' action

狂风中的少年 提交于 2019-12-08 05:26:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!