saved_post post_updated自定义字段更新之前触发,自定义字段更新后触发什么函数?文字新闻


saved_post post_updated firing before custom fields are updated, what function fires after custom fields are updated? wordpress

我有一个问题,我想使用另一个自定义帖子字段更新自定义用户元数据字段。它必须在包括自定义字段在内的整个帖子保存/更新之后发生。但是我找到的所有调用:更新基本帖子数据,然后调用操作,然后更新自定义字段。

saved_post & post_updated 导致此代码延迟 1 保存。 即,如果我要发布一个新帖子并将 $my_value 设置为 5,第一次保存它会返回 0,然后下次它会返回 5。

有谁知道在保存自定义帖子数据后运行的动作钩子? 或者如何在自定义帖子数据之后触发save_post或post_updated?

    function zhu_li_do_the_thing( $post_id ) {

获取帖子数据,提取作者 ID 和帖子类型

$post = get_post( $post_id );
$post_type = $post->post_type;
$userid = $post->post_author;
/

/如果你的自定义帖子类型,那么获取我想要的自定义字段值。

if( 'my_custom_post_type' == $post_type ){
  $post_custom_fields = get_post_custom($post_id);
  $my_custom_field = $custom_fields['im_a_field'];
  foreach($im_a_field as $key ){
        $my_value = $key;
        }
如果值

以"+"或"-"开头,则对作者自定义元数据进行数学运算,如果为空,请忽略它,如果它是其他任何内容,则使元数据=值。

  if(substr($my_value, 0,1)=='+' || substr($my_value, 0,1)=='-'){
        $my_int = intval($my_value);
        $author_int = intval(get_user_meta($userid, 'the_objective, true)); 
        $result = ($author_int + $str);
        update_user_meta( $userid, 'the_objective', $result );
  }elseif($my_value == null ){
        return;
  }else{ 
        update_user_meta( $userid, 'the_objective', $my_value )
  }}};
add_action( 'save_post, 'zhu_li_do_the_thing' );
您应该使用 pre_post_update 之前检查帖子元

,并使用post_updated检查更新的帖子元。

以下是来自一个类的代码片段,用于检查 woocomerce 产品的库存状态。

function __construct()
{   
    add_action( 'post_updated', array( $this, 'post_updated' ), 1, 1 );
    add_action( 'pre_post_update', array( $this, 'pre_post_update' ), 1, 1 );
}
function pre_post_update( $post_ID )
{       
    global $post_type;
    if( $post_type == 'product' )
        define( 'stock_status_previous', get_post_meta( $post_ID, '_stock_status', 1 ) );
}
function post_updated( $post_ID )
{
    if( defined( 'stock_status_previous' ) )
    {
        $stock_status = get_post_meta( $post_ID, '_stock_status', 1 );
        if( $stock_status == stock_status_check )
            return;
        // post meta is different - sp do something
}