Support

Account

Home Forums Add-ons Repeater Field Update/ Create new repeater block with foreach loop

Solved

Update/ Create new repeater block with foreach loop

  • Hi,
    I am followed instruction from this link:
    https://www.advancedcustomfields.com/resources/update_field/
    and i am using this code to create repeater blocks for each item using this code:

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

    The field is created in the backend but the problem is its creating only one block when it should create depending upon items. they could be one or two or many. I am using the following code to accomplish this:

    //Create ACF Repeater blocks 
            if($orig_cart_items){
                foreach ($orig_cart_items as $item){
                    $field_key = "field_59aff2511ca92";
                    $value = array(
                    	array(
                    		"festival_id"	=> $item['show_festival_id'],
                    		"edition_id"	=> $item['show_edition_id'],
                            "movie_id"	    => $item['show_movie_id']
                    	)
                    );
                    update_field( $field_key, $value, $post_id );
                }
            }    
  • 
    if ($orig_cart_items) {
      $field_key = "field_59aff2511ca92";
      $value = array();
      foreach ($orig_cart_items as $item) {
        $value[] = array(
          'festival_id' => $item['show_festival_id'],
          'edition_id" => $item['show_edition_id'],
          'movie_id' => $item['show_movie_id']
        );
      } // end foreach
      update_field($field_key, $value, $post_id);
    } // end if
    
  • $academic_key = “field_616409d2c5515”;
    $value = array();
    foreach ($_POST as $feature_value) {
    $value[] = array(
    //acf name form
    “name_of_high_school” => $feature_value[‘school_name’],
    “city” => $feature_value[‘city_name’],
    “country” => $feature_value[‘country_name’],
    “graduation_year” => $feature_value[‘graduation_year’],
    “starting_graduation_month” => $feature_value[‘academic_starting_month_year’],
    “ending_graduation_month” => $feature_value[‘academic_ending_month_year’],
    “curriculum” => $feature_value[‘academic_curriculum’],
    “classes” => $feature_value[‘academic_classes’],
    “standardized_test_scores” => $feature_value[‘academic_score’],
    “awards” => $feature_value[‘academic_awards’],
    “grades” => $feature_value[‘academic_grades’],
    “others” => $feature_value[‘academic_other’]
    );
    }
    update_field($academic_key, $value, $post_id);

    If have the same issue

  • $_POST it retrieve all form data

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

The topic ‘Update/ Create new repeater block with foreach loop’ is closed to new replies.