Home Forums Add-ons Gallery Field Insert image into ACF Gallery on Save not working


Insert image into ACF Gallery on Save not working

  • I am building a front end form and if the user does not provide any images we want to populate a gallery with one image from a given set of images. The set of images are themselves a gallery in the theme options. I can grab the id of the image I want to add, and update the field, but the image does not show up in the edit view of the post. When I query that field for the post immediately after the update, the image id is returned, but its not showing up in the gallery field. If I do another save, the gallery field registers as empty.

    Saving to another image field works just fine, but not a gallery.

    function assign_random_image($post_id) {
      $images = get_field('farm_images', 'options', false); //this is the set of images to choose from
      $random = array_rand( $images, 1); 
      $img_id = $images[$random];
      //add random image the farm photos gallery field - does not work!
      $result = update_field('field_5a84af276ac3b', array($img_id), $post_id);
      //test image field - this one works!
      $result = update_field('field_5a9ddee6df01b', $img_id, $post_id);
      $gallery = get_field('farm_photos', $post_id, false);
      error_log(print_r($gallery, true)); //this returns the $img_id
    function acf_save_farm( $post_id ) {
      $type = get_post_type($post_id);
      if ($type == 'farm') {
        $images = get_field('farm_photos', $post_id); //this returns empty on subsequent saves
        if (empty($images)) {
          $img = assign_random_image($post_id);
    add_action('acf/save_post', 'acf_save_farm', 20);
  • Ive narrowed this down to a problem with BuddyForms. If I remove the field from the frontend form it works, but when I add it back in, it does not…

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

The topic ‘Insert image into ACF Gallery on Save not working’ is closed to new replies.