Support

Account

Home Forums Add-ons Repeater Field Repeater update_field

Solved

Repeater update_field

  • $i = 1;
    foreach ($wpbb_xml_params->features->children() as $second_gen) {
        $feature_value = (string)$second_gen;
        $value[$i] = array( 'sub_field_' . $i => $feature_value );
        $i++;
    }
    update_field( 'field_59606db8525db', $value, $wpbb_job_post_id );

    I’ve no idea why this isn’t working. It creates 5 rows but doesn’t fill with values. I struggle with the repeater field in the past. My features array looks like below, it’s from an XML file and i’m using SimpleXML to traverse it all.

        <features>
            <feature>1st Floor Maisonette</feature>
            <feature>2 Double Bedrooms</feature>
            <feature>No Onward Chain</feature>
            <feature>Share of Freehold</feature>
            <feature>Private Rear Garden</feature>
        </features>

    And this is my field setup for features:

            array (
                'key' => 'field_59606db8525db',
                'label' => 'Features',
                'name' => 'features',
                'type' => 'repeater',
                'instructions' => '',
                'required' => 0,
                'conditional_logic' => 0,
                'wrapper' => array (
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'collapsed' => '',
                'min' => 0,
                'max' => 0,
                'layout' => 'table',
                'button_label' => 'Add Row',
                'sub_fields' => array (
                    array (
                        'key' => 'field_59606dc9525dc',
                        'label' => 'Feature',
                        'name' => 'feature',
                        'type' => 'text',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array (
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '',
                        'placeholder' => '',
                        'prepend' => '',
                        'append' => '',
                        'maxlength' => '',
                        'readonly' => 0,
                        'disabled' => 0,
                    ),
                ),
            ),
  • the array for your repeater should look like this

    
    $value = array(
      // nested for each row
      array(
        // field key => value pairs
        'field_59606dc9525dc' => 'value for row 1'
      ),
      array(
        // field key => value pairs
        'field_59606dc9525dc' => 'value for row 2'
      ),
    );
    

    I think that this will do it

    
    foreach ($wpbb_xml_params->features->children() as $second_gen) {
        $feature_value = (string)$second_gen;
        $value[] = array( 'field_59606dc9525dc' => $feature_value );
    }
    update_field( 'field_59606db8525db', $value, $wpbb_job_post_id );
    
  • Thank you! It works perfectly. I do find the update_field() documentation for repeater fields confusing…

    // 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 );
  • I think that sub_field_1 and sub_field_2 in that example are supposed to represent the field names of the sub fields, but using the field keys is always the better option when using update_field(). The second example in the same code for flex fields better illustrates the way the value array needs to be for repeaters, with the exception of the acf_fc_layout index. Repeaters and flex fields work the same way with the exception of the layout name.

  • I am having a little bit of trouble getting this to work, I have similar issue but maybe I am missing something. I have frontend jquery repeater field with name arrays which I try to populate. The rows are formed in the ACF but no value is stored.

    Below is the code.

     
    // Work Experience
            $job_title = $_POST['job_title'];
    
            foreach($job_title as $i => $jt ):
                // Job title
                // $job_title = $_POST['job_title'];
                // Reference
                $reference = $_POST['reference'];
                // Reference_contact
                $reference_contact = $_POST['reference_contact'];
                // Work Experience Textarea
                $work_experience_textarea = $_POST['work_experience_textarea'];
    
                echo $jt;
                echo $reference[$i];
                echo $reference_contact[$i];
                echo $work_experience_textarea[$i];
    
                $value_work_experience[$i] = array(
                    array(
                        "field_5ddda6cf10b45" => $jt,
                        "field_5ddda6db10b46" => $reference[$i],
                        "field_5ddda6f310b47" => $reference_contact[$i],
                        "field_5ddda70010b48" => $work_experience_textarea[$i]
                    ),
                );
            endforeach;
    update_field( $field_key_work_experience, $value_work_experience, $post_id ); // work experience
    
  • You do not need the nested array in your case because you are using [] which creates the first level of the nesting for each row.

    
    // Work Experience
            $job_title = $_POST['job_title'];
    
            foreach($job_title as $i => $jt ):
                // Job title
                // $job_title = $_POST['job_title'];
                // Reference
                $reference = $_POST['reference'];
                // Reference_contact
                $reference_contact = $_POST['reference_contact'];
                // Work Experience Textarea
                $work_experience_textarea = $_POST['work_experience_textarea'];
    
                echo $jt;
                echo $reference[$i];
                echo $reference_contact[$i];
                echo $work_experience_textarea[$i];
    
                $value_work_experience[$i] = array(
                        "field_5ddda6cf10b45" => $jt,
                        "field_5ddda6db10b46" => $reference[$i],
                        "field_5ddda6f310b47" => $reference_contact[$i],
                        "field_5ddda70010b48" => $work_experience_textarea[$i]
                );
            endforeach;
    update_field( $field_key_work_experience, $value_work_experience, $post_id ); // work experience
    
  • Hi John,

    thanks for the answer, it works!

  • I’ve got the sub fields in the first repeater posting fine. But, I can’t quite figure out how to get the nested repeater working properly.

    Here’s the code I have so far:

    $event_field_key = ‘field_535e6b9ffe3da’;

    $events[] = array(
    ‘start-date’ => $startDate,
    ‘end-date’ => $endDate,
    ‘notes’ => $_POST[‘p’.$p.’-notes’],
    ‘start-end-times’ => array(
    ‘start-time’ => ’09:00′, // would be dynamic
    ‘end-time’ => ’17:00′ // would be dynamic
    )
    );

    update_field($event_field_key, $events, $post_id);
    I’m not sure if I can just nest another array in there, or if I need to do something else.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Repeater update_field’ is closed to new replies.