Home › Forums › Front-end Issues › 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();
}
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
❓Ever wondered when and why ACF uses JSON instead of the database? Check out our summary of the most recent session of ACF Chat Friday for the answer, and make sure to register for the next session.
— Advanced Custom Fields (@wp_acf) February 23, 2023
👉 https://t.co/3UtvQbDwNmhttps://t.co/wfFEVcXVKc
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.