Support

Account

Home Forums General Issues add_row doesn't work the first time

Solving

add_row doesn't work the first time

  • After creating a post, it is’t possible to add new value to the “repeater”.
    You need to open the admin panel and just click update post. And after that add new value to the “repeater”

    acf structure

    1. field group – (group_main_info)
    1.2 repeater – (phones)

    Creater post

    	$post_data = array(
    		'post_title'   => $data["main_info"]["company"],
    		'post_content' => "",
    		'post_status'  => 'publish',
    		'post_author'  => get_current_user_id(),
    		"post_type"    => "companies"
    	);
    
    // Insers post to DB
    	$the_record_id = wp_insert_post( $post_data );

    If you run this part of the code after you have logged into the admin panel, opened the entry and simply clicked update. All data is added perfectly.
    If this is not done, but simply as a series of actions does not work.

    add_row("group_main_info_phones", ["name"=>"Jon","phone"=>"0897654007"], $the_record_id);

    if you create a post using the wp_insert_post() function in the database, there is no such field.

    And if you just open the post, press “update” immediately, then this field appears in the system and then “add_row” works

    How can I fix it so that the data automatically appears in the repeater immediately after the creation of the record?

  • You need to use the field key.

    When a field does not already exist and have a value for an ACF field then ACF is unable to figure out how to update the field.

    This is true of all ACF functions for updating values. See the section “Updating via field key” on https://www.advancedcustomfields.com/resources/update_field/

    In the case of a repeater all of the sub field values you are adding to the row should also use the field key.

  • Dear, John Huebner!

    Why does it work like this?

    Structure:

    1. repeater – repeater_test

    	$post_data = array(
    		'post_title'   => $data["main_info"]["company"],
    		'post_content' => "",
    		'post_status'  => 'publish',
    		'post_author'  => get_current_user_id(),
    		"post_type"    => "companies"
    	);
    
    	$the_record_id = wp_insert_post( $post_data );
    
    	add_row("repeater_test", ["test"=>"adding to the row should"], $the_record_id);

    But it doesn’t work like this

    Structure

    1. field group – (group_main_info)
    1.2 repeater – (phones)

    add_row("group_main_info_phones", ["name"=>"Jon","phone"=>"0897654007"], $the_record_id);

    I tried to write element keys and it doesn’t work the same way.

  • Because you have a repeater inside of a repeater. A group field is a repeater that always has a single row. You cannot refer to the repeater with the name “group_main_info_phones” to update the field.

    Like I said, you need to use field keys

    
    $value = array(
      // field_XXXXXXX is the field key of your nested repeater
      // it's value is an array of rows for the repeater
      'field_XXXXXXX' => array(
        // each row of the repeater is a nested array
        // that holds field key => value pairs
        array(
          // field_YYYYYYY is the field key of "name" field
          'field_YYYYYYY' => 'Jon',
          // field_ZZZZZZZ  is the field key of "phone" field
          'field_YYYYYYY' => '0897654007'
        )
      )
    );
    // field_AAAAAAA is the field key of the group field 
    update_field('field_AAAAAAA', $value, $the_record_id);
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.