Support

Account

Home Forums General Issues Load default value for gallery field

Solved

Load default value for gallery field

  • Hi there,
    I know how to pre-fill fields like text, WYSIWG or files, but struggle with the gallery field (image array) being pre-filled by a field from an Options page, like so:

    function default_photos( $field_photos ) {
    		
         $photos = get_field('artist_photos', 'option');
    		
         $field_photos['default_value'] = $photos;
         return $field_photos;
    		 
    }
    
    add_filter('acf/load_field/name=press_photos', 'default_photos');

    I believe this has something to do with the field being saved as an array and expecting IDs to be used? Anyone an idea how to achieve a proper pre-fill as default for this field?

    Thanks a lot in advance 😉

  • And I just solved it myself. You need to create an array and fill it with each and every file’s ID, off you go:

    function default_photos( $field_photos ) {
    		
        $photos = get_field('artist_photos', 'option');
    	
    	$array = array();
    
    	foreach ($photos as $photo) {
    		$id = $photo['ID'];
    		array_push($array, $id);
    	};
    		
         $field_photos['default_value'] = $array;
         return $field_photos;
    		 
    }
    
    add_filter('acf/load_field/name=press_photos', 'default_photos');
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.