Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在更新自定义字段之前已保存\u post\u更新后触发,在更新自定义字段之后触发什么函数?wordpress_Php_Wordpress_Custom Post Type - Fatal编程技术网

Php 在更新自定义字段之前已保存\u post\u更新后触发,在更新自定义字段之后触发什么函数?wordpress

Php 在更新自定义字段之前已保存\u post\u更新后触发,在更新自定义字段之后触发什么函数?wordpress,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,我有一个问题,我想使用另一个自定义post字段更新自定义用户元数据字段。它必须在包括自定义字段在内的整个帖子保存/更新后发生。但是我找到的所有调用:更新基本post数据,然后调用操作,然后更新自定义字段 保存的\u post&post\u更新会导致此代码延迟1次保存。也就是说,如果我写一篇新文章并将$my_值设置为5,我第一次保存它时,它会返回0,然后下一次它会返回5。等等 有人知道在保存自定义post数据后运行的操作挂钩吗?或者如何在自定义post数据后使save_post或post_更新为f

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

保存的\u post&post\u更新会导致此代码延迟1次保存。也就是说,如果我写一篇新文章并将$my_值设置为5,我第一次保存它时,它会返回0,然后下一次它会返回5。等等

有人知道在保存自定义post数据后运行的操作挂钩吗?或者如何在自定义post数据后使save_post或post_更新为fire

    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;
        }
//如果该值以“+”或“-”开头,请根据自定义元数据对其进行计算,如果为null,则忽略它,如果是其他值,则使元数据=该值

  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\u post\u update
检查之前的帖子元,并使用
post\u updated
检查更新后的帖子元

下面是检查Woocomece产品库存状态的类的代码片段

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

}