Support

Account

Home Forums Add-ons Gallery Field Programmatically Inserting Photos Into ACF Gallery Field, But…. Reply To: Programmatically Inserting Photos Into ACF Gallery Field, But….

  • A gallery field in ACF stores an array of image IDs. I believe that the following line overwrites what’s stored with the new value each time.

    
    update_field("field_5693402ab8561", $attach_id, $post[0]->ID );
    

    You will need to get what’s already in the field, append the new image to the array and then update it. Something like this might work.

    
    $array = get_field('field_5693402ab8561', $post[0]->ID, false);
    if (!is_array($array)) {
      $array = array();
    }
    $array[] = $attach_id;
    update_field('field_5693402ab8561', $array, $post[0]->ID );