wordpress 高级自定义字段专业中继器和自定义保存功能

up9lanfz  于 2023-10-16  发布在  WordPress
关注(0)|答案(1)|浏览(135)

我试图建立一个自定义的形式,应该允许用户保存自定义职位的数据库。一切都很顺利,但中继场给我带来了一些问题。ACF支持无法帮助我,我实现的解决方案遵循了指南中的指示。问题是:

问题:

我有一个ACF repeater字段,它位于一个组中(实际结构中也有一些条件逻辑,但它不应该干扰保存过程),具有以下结构

GROUP "services_group"

   Repeater field with Key **field_64d6315b78628** slug "services"

       - GROUP sub-field with slug "service_details"
           __ SELECT with slug "service_type"
           __ SELECT with slug "service_one"
           __ SELECT with slug "subservice_two"
           __ SELECT with slug "subservice_three"

       - TEXTAREA with slug "description"

文档在这里:https://www.advancedcustomfields.com/resources/update_field/#saving-values-to-a-new-post

我已经实现了以下解决方案(不工作):

$post_id = 753;
$repeater_key = 'field_64d6315b78628';    //Repeater key
$values = [
   [
      'service_details' => [              //Group with subfields
         'service_type' => 'R&D',
         'subservice_one' => 'Applied Research',
         'subservice_two' => '',
         'subservice_three' => '',
      ],
      'description' => 'Test description',
   ]
];

update_field($repeater_key, $values, $post_id);

上面的代码不工作,也不抛出错误,数据库中没有写入任何内容。

3bygqnnd

3bygqnnd1#

我已经找到解决办法了。问题是中继器在一个组内。为了正确地保存repeater,我必须将它封装在父组中,同时也保存它,指定整个关系树如下:

$post_id = 753;
$group_key = 'field_64d6311b78627';    //Services_group key

$values =
   [
      'services' => [                  //Repeater slug
         [
            'service_details' => [     //Group with subfields
               'service_type' => 'R&D',
               'subservice_one' => 'Applied Research',
               'subservice_two' => '',
               'subservice_three' => '',
            ],
            'description' => 'Test description',
         ],
      ]
   ];

update_field($group_key, $values, $post_id);

相关问题