Support

Account

Home Forums General Issues Insert rows in repeater field which is in nested groups.

Solved

Insert rows in repeater field which is in nested groups.

  • Hello!

    I have interesting problem and don’t know how to solve it anymore, so I am hoping for your help.

    I have group in which is another group and in this last group I have repeater field. In this repeated field I can’t add any row if I don’t go to post and press update button manually. Maybe it’s worth to mention that posts are created by wp_insert_post function.

    This code works only if I go manually to administration panel and press update button:

    delete_post_meta($company->ID, 'public_information_expertise_in_numbers_numbers');
    foreach ($number_values as $key => $number_value) {
        update_row('public_information_expertise_in_numbers_numbers', $key + 1, array(
            'number' => $number_value,
            'suffix' => $number_suffix[$key],
            'text' => $number_text[$key]
        ), $company->ID);
    }

    public_information, expertise_in_numbers are groups and numbers is repeater field.

    Thank you!

  • You said that you are inserting a post. This means that the ACF fields have no values.

    update_row() will have no effect because there is no data to update, you would normally need to use add_row(). But you are dealing with nested fields so you would probably have to use add_sub_row(). But to add a sub row the row would have to exist first. In other words the fields public_information and expertise_in_numbers would have to exist before you try to add a row to numbers

    Also, because the values to not yet exist you would need to use field keys instead of field names.

    This would all get very complicated because of the nesting and then need to create the parent fields before you can add a row to the nested field. Instead I would use update_field() and I would build the array needed for the top level field.

    
    // a simple example
    $public_information = 'field_XXXXX'; // field key for public_information
    $expertise_in_numbers = 'field_YYYYY'; // field key for expertise_in_numbers
    $numbers = 'filed_ZZZZZ'; // field key for numbers
    
    $numbers_array = array();
    foreach ($number_values as $key => $number_value) {
      $numbers_array[] = array(
        // field key value pairs of repeater sub feilds
        'field_AAAAA' => $number_value,
        'field_BBBBB' => $number_suffix[$key],
        'field_CCCCC' => $number_text[$key]
      );
    }
    
    $value = array(
      $expertise_in_numbers  => array(
        $numbers => $numbers_array;
      }
    )
    update_field($public_information, $value, $post_id);
    
  • Thank you very much! Your example is working perfect and now I understand what to do in these case of situations.

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

You must be logged in to reply to this topic.