wordpresswordpress-themingadvanced-custom-fieldsacfpro

Advanced Custom Fields pro repeater and custom save functionality


I am trying to build a custom form that should allow users to save custom posts to the database. Everything is smooth, but a repeater field is giving me some problems. ACF support wasn't able to help me, and the solution that I have implemented follows what the guide indicates. Here is the issue:

Problem:

I have an ACF repeater field that sits inside a group (the actual structure has some conditional logic in place too, but it shouldn't interfere with the saving process) that has the following structure

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"

The documentation is here: https://www.advancedcustomfields.com/resources/update_field/#saving-values-to-a-new-post

I have implemented the following solution (not working):


$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);

The above code doesn't work, and doesn't throw an error, nothing get written in the database.


Solution

  • I have found the solution. The problem is that the repeater is inside a group. To correctly save the repeater I had to encapsulate it inside the parent group while saving it too, specifying the whole relation tree as follow:

    $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);