wordpress 将 post_status 设置为“草稿";在“save_post"操作中 [英] wordpress set post_status as "draft" in 'save_post' action

查看:33
本文介绍了wordpress 将 post_status 设置为“草稿";在“save_post"操作中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个适用于我的自定义帖子类型的自定义函数.在处理 save_post 动作时:

I have a custom function that works with my custom post type. While porocessing save_post action:

add_action( 'save_post', 'my_custom_function' );

我想将帖子状态设置为草稿(以防从外部 api 获取自定义数据时出现问题).在我的 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 );
}

但看起来,在处理 save_post 之后,post_status 会再次被设置.

but it seems, that after save_post is being processed, then post_status is being set again.

任何人都有一个想法,我应该在哪里挂钩,所以在保存帖子数据时,我可以修改它的 post_statuspost_date 和其他一些帖子数据信息,这样它们就不会被矫枉过正?

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?

推荐答案

你应该把它挂到 <代码>wp_insert_post_data.然后你可以使用这样的函数将你的帖子状态设置为草稿:

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;
}

这篇关于wordpress 将 post_status 设置为“草稿";在“save_post"操作中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆