My HTML form allows users to select any number of images to be uploaded to a custom post type. I've created a repeater field in ACF called 'images', of 'image' objects.
How do I save the uploaded images into that repeater field?
This is what I currently have:
$files = $_FILES['file'];
$image_number = 1;
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$uploaded_file = wp_handle_upload($file, array('test_form' => FALSE));
update_sub_field( array('images', $image_number++, 'image'), $uploaded_file, $post_id);
}
}
But the images aren't saved. Any help appreciated, thanks!
As far as I see, your use of the update_field
function especially it's signature is wrong. You mixed up the parameters. Have a look here: Wordpress ACF: How to add rows to a repeater field attached to a user via custom code (PHP)
Try the following code. It is working for me. Although it's an old post, it might help someone else in future.
$attachment = array(
'guid' => $upload_dir . '/' . basename( $filename ),
'post_mime_type' => $filetype['type']
);
$attachment_id = wp_insert_attachment($attachment, $filename, $parent_post_id);
$row = array(
'name' => "Hello",
'logo' => $attachment_id, // this is where you put post id of your image
'website' => "http://hello.com"
);
add_row( "repeater_field", $row, $blog_post_id);
I also noticed that add_row works only if you have already at least one row. But if you want to add a new row, use update_field. Take a look here: add_row in ACF Pro isn't saving repeater values
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.