首页 > 解决方案 > ACF 转发器字段覆盖而不是更新

问题描述

我有一个 API,我在其中通过 php 从门户将模型的投资组合图像提取到 Wordpress 中的自定义帖子类型中。它会拉入第一张图片,然后我像这样在 Wordpress 中手动上传更多图片。

在此处输入图像描述

但是,当我再次获取 API 时,它会覆盖整个投资组合而不是更新它!!!

这是代码..

    private function set_talent_contents($talent, $post_id)
{

    $terms = $talent['www'];
    $gender = $talent['gender'];

    if($this->actions['full_content'] && $talent['pic_1'] && $talent['pic_1'] != 'http://portal.pha-agency.co.uk/models/' )
    {

        $attachments = $this->uploadRemoteImageAndAttach($talent['pic_1'], $post_id);

        $pictures[] = [ 'image' => $attachments ];



        if( $this->actions['feature'] )
        {
            $output['feature'] = '✓';

            set_post_thumbnail( $post_id, $attachments );
        }


    }else{
        $pictures = null;
    }




    $headshot = (isset($pictures[0]['image']) ? $pictures[0]['image'] : null);

    $videos = [];

    $polaroids = [];



    if($this->actions['full_content'])
    {

        update_field($this->field_mapping['default']['headshot'], $headshot, $post_id);
        update_field($this->field_mapping['default']['portfolio'], $pictures, $post_id);
        update_field($this->field_mapping['default']['videos'], $videos, $post_id);
        update_field($this->field_mapping['default']['polaroids'], $polaroids, $post_id);
    }

    update_field($this->field_mapping['default']['header'], $this->field_mapping['default']['header_default'], $post_id);



    $talent_cats = array_filter(explode(',', $terms));

    if($talent_cats)
    {
        foreach($talent_cats as $cat)
        {
            $form_type = '';

            // just incase its something like 'Male Model'. we can strip the gender off and try the gender seperatly
            $cat = strtolower(trim(str_replace($gender, '', $cat)));

            if(isset($this->field_mapping[$cat]))
            {
               $form_type = $cat;

            }elseif(str_contains($cat, 'Sports') ){
                $form_type = 'sports';

            }elseif(str_contains($cat, 'model') || str_contains($cat, 'kids')){
                $form_type = 'models';

            }elseif(str_contains($cat, 'actor')){

                $form_type = 'casting';
            }else{
                continue;
            }


            if($this->actions['full_content'])
            {
                update_field($this->field_mapping[$form_type]['header'], $this->field_mapping[$form_type]['header_default'], $post_id);

                update_field($this->field_mapping[$form_type]['headshot'], $headshot, $post_id);
                update_field($this->field_mapping[$form_type]['portfolio'], $pictures, $post_id);
                update_field($this->field_mapping[$form_type]['videos'], $videos, $post_id);
                update_field($this->field_mapping[$form_type]['polaroids'], $polaroids, $post_id);
            }





        }
    }

现在,我继承了这段代码,需要让它正常工作。我已与 ACF 取得联系,以查看它是否是转发器字段中的错误,这是响应:

    // save a repeater field value
   $field_key = "field_12345678";
   $value = array(
    array(
        "sub_field_1"   => "Foo",
        "sub_field_2"   => "Bar"
    )
   );
   update_field( $field_key, $value, $post_id );

现在我只是不确定如何实现它!

标签: phpwordpressadvanced-custom-fields

解决方案


推荐阅读