You are updating the same post_meta
on each loop. Therefore you are probably saving the last uploaded image only. Try something like this (simplified):
foreach ($_FILES['wp_custom_attachment']["tmp_name"] as $key => $value) {
$post_meta = get_post_meta($id, 'wp_custom_attachment', $upload);
if( $post_meta ) {
$post_meta .= ', ' . $upload;
update_post_meta($id, 'wp_custom_attachment', $post_meta);
}
else {
add_post_meta($id, 'wp_custom_attachment', $upload);
}
}
First I check if the post_meta
exists. If not, I create it with the value of $upload. If it does exist, I add the new $upload
to the existing value in $post_meta
. When you will need to get all the values, you can use explode()
.
$images = explode( ',', $post_meta );
foreach( $images as $img ) {
// bla...
}
Another way you can do it is by creating different unique keys for each upload and looping all over them.
foreach ($_FILES['wp_custom_attachment']["tmp_name"] as $key => $value) {
$post_meta = get_post_meta($id, 'wp_custom_attachment_1', $upload);
if( !$post_meta ) {
add_post_meta($id, 'wp_custom_attachment_1', $upload);
}
else {
$counter = 1;
while( $flag !== true ) {
$post_meta = get_post_meta($id, 'wp_custom_attachment_' . $counter, $upload);
if( $post_meta ) {
continue;
}
else {
add_post_meta($id, 'wp_custom_attachment_' . $counter, $upload);
}
$counter++;
}
}
}
The code is raw and simplified.