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.

    • mabuli

    • January 26, 2020 at 4:05 am

    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
    
    • mabuli

    • January 26, 2020 at 10:24 pm

    Hi John,

    thanks for the answer, it works!

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.