使用save_post动作(自定义帖子类型)在函数中操作自定义字段(在acf中创建)以更新元

时间:2018-10-24 09:59:09

标签: php wordpress function

在woocommerce的自定义帖子类型产品中,我有一些自定义字段(在ACF中创建),在保存或更新帖子时需要采取措施,即我想​​添加字段的值在woocommerce属性中。 这是我的示例代码。我有一个文本字段:ISBN,我尝试在meta属性中传递值:

//save_post_NAME_CPT
add_action('save_post_product', 'update_post_meta_subito' , 10, 3 ); 

function update_post_meta_subito( $post_id, $post, $update ) { 

        //get field isbn of current post
        $prova_termine = get_field( "isbn", $post_id );

         //add value field in global pa_autore attribute
        $term_taxonomy_ids = wp_set_object_terms( $post_id, $prova_termine, 'pa_autore', true );

        //add value field in current attribute post
        $data = Array(

        'pa_autore'=>Array( 
        'name'=>'pa_autore', 
        'value'=>$prova_termine, 
        'is_visible' => '1', 
        'is_variation' => '1',
        'is_taxonomy' => '1'
        ),

        );

        update_post_meta( $post_id, '_product_attributes',$data);
    }

这不起作用!它没有传递任何价值!但在这种情况下,它可以工作:

add_action('save_post_product', 'update_post_meta_subito' , 10, 3 ); 

function update_post_meta_subito( $post_id, $post, $update ) { 

        $prova_termine='ronaldo';

        $term_taxonomy_ids = wp_set_object_terms( $post_id, $prova_termine, 'pa_autore', true );

        $data = Array(

        'pa_autore'=>Array( 
        'name'=>'pa_autore', 
        'value'=>$prova_termine, 
        'is_visible' => '1', 
        'is_variation' => '1',
        'is_taxonomy' => '1'
        ),

        );

        update_post_meta( $post_id, '_product_attributes',$data);
    }

那么,在第一个函数中不采用该字段的值,我在哪里错了?

0 个答案:

没有答案