Support

Account

Home Forums Front-end Issues Image not attached to post on save acf form Reply To: Image not attached to post on save acf form

  • John Huebner’s reply and this post sent me into the right direction:
    https://support.advancedcustomfields.com/forums/topic/acf_form-attachment-to-media-gallery/

    Modified it for gallery field.

    
    function my_save_post( $post_id ) {
            //attachment to media gallery 
    	$my_files = get_field('my_gallery', $post_id);	
    
    	if($my_files){
    
    		foreach( $my_files as $my_file ):
    			$file_id = $my_file['ID'];
    			$post_to_update = array(
    				'ID'           => $file_id,
    				'post_parent'  => $post_id
    			);
    			wp_update_post( $post_to_update );
    		endforeach;
    	}
    }
    add_action('acf/save_post', 'my_save_post', 15);