Support

Account

Home Forums Front-end Issues Insert new files to existing acf gallery, but wiped old data.

Solved

Insert new files to existing acf gallery, but wiped old data.

  • Insert new files to existing acf gallery, but old data completely removed and insert new files. How can i keep old files?
    Here i am using insert_multiple_attachment this function to add images. But it completely wiped old data.

    <form method="POST" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" enctype="multipart/form-data">
        <div class="form-group">
            <label>Files</label><br>
            <input type="file" class="form-input" name="filesnew[]" multiple="">
            <small>Press Ctrl key to select multiple</small>
        </div>
        <div class="form-group">
            <input type="submit" name="submit" id="submit" class="form-submit" value="Add New Item" />
        </div>
    </form>
    <?php
    if(!empty($_FILES['filesnew']['name'][0]))
    {
        $filesbook = $_FILES['filesnew'];
        insert_multiple_attachment($filesbook, $pid, 'file_books');
    }
    
    function insert_multiple_attachment($files,$post_id,$imfield)
    {
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');
        $count = 0;
        $galleryImages = array();
        foreach ($files['name'] as $count => $value)
        {
            if ($files['name'][$count])
            {
                $file = array(
                    'name'     => $files['name'][$count],
                    'type'     => $files['type'][$count],
                    'tmp_name' => $files['tmp_name'][$count],
                    'error'    => $files['error'][$count],
                    'size'     => $files['size'][$count]
                );
                $upload_overrides = array( 'test_form' => false );
                $upload = wp_handle_upload($file, $upload_overrides);
                $filename = $upload['file'];
                $parent_post_id = $post_id;
                $filetype = wp_check_filetype( basename( $filename ), null );
                $wp_upload_dir = wp_upload_dir();
                $attachment = array(
                    'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
                    'post_mime_type' => $filetype['type'],
                    'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                    'post_content'   => '',
                    'post_status'    => 'inherit'
                );
                $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
                require_once( ABSPATH . 'wp-admin/includes/image.php' );
                $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
                wp_update_attachment_metadata( $attach_id, $attach_data );
                array_push($galleryImages, $attach_id);
            }
            $count++;
            update_field($imfield, $galleryImages, $post_id);
        }
    }
    ?>
  • You need to get the values that are already saved and then add to them

    instead of

    
    $galleryImages = array();
    

    use

    
    $galleryImages = get_field(($imfield, $post_id, false);
    if (empty($galleryImages)) {
      galleryImages = array();
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.