If you want the image's URL to be different, you need to first copy the image file to the user-specific folder and then create a new attachment for that copied image — i.e. clone the image's "post" data.
So replace this:
// Get the attachments (IDs) attached to the `property` CPT.
$prop_att_ids = (array) get_post_meta( $propertyID, 'imic_property_sights', false );
// Copy the meta value (attachments) to the `v_editor` CPT.
$vid_pix = get_post_meta( $vidID, 'vid_pix', false ); // test
//delete_post_meta( $vidID, 'vid_pix' );
foreach ( $prop_att_ids as $att_id ) {
... including the other code here ...
}
$vid_pix2 = get_post_meta( $vidID, 'vid_pix', false ); // test
//var_dump( $vid_pix, $vid_pix2, $url ); // test
with this one:
$path2 = $yourID . '/' . $vidID; // relative to wp-content/uploads
$prop_att_ids = (array) get_post_meta( $propertyID, 'imic_property_sights', false );
foreach ( $prop_att_ids as $att_id ) {
// Check if we have a valid image/attachment.
if ( $att_id && $file = get_attached_file( $att_id ) ) {
$filename = basename( $file );
$file2 = $path . '/' . wp_unique_filename( $path, $filename );
// Copy the image file to $path.
if ( @copy( $file, $file2 ) ) {
// Copy the attachment (post) data.
$att = get_post( $att_id, ARRAY_A );
unset( $att['ID'] );
$att_id2 = wp_insert_attachment( $att, $file2 );
// Then add the meta data `vid_pix`.
add_post_meta( $vidID, 'vid_pix', $att_id2 );
// Copy the attachment's meta data. (no thumbnails)
$data = wp_get_attachment_metadata( $att_id );
$data['file'] = $path2 . '/' . basename( $file2 );
$data['sizes'] = [];
wp_update_attachment_metadata( $att_id2, $data );
}
}
}
Note that since we're creating a new attachment post, I used wp_unique_filename()
to generate a unique file name for the image file that we're copying to the $path
.
UPDATE
If you want to generate all thumbnails, replace this part:
// Copy the attachment's meta data. (no thumbnails)
$data = wp_get_attachment_metadata( $att_id );
$data['file'] = $path2 . '/' . basename( $file2 );
$data['sizes'] = [];
wp_update_attachment_metadata( $att_id2, $data );
with this one:
// Create the attachment's meta data. (with thumbnails)
$att2 = get_post( $att_id2 );
wp_maybe_generate_attachment_metadata( $att2 );
UPDATE #2
Here's the full code I used:
<?php
// File: import-pics.php
require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
global $current_user, $imic_options; // Use global
wp_get_current_user(); // Make sure global is set, if not set it.
$yourID = $current_user->ID;
$propertyID = $_POST['propertyID'];
$vidID = $_POST['videoID'];
$path = WP_CONTENT_DIR . '/uploads/' . $yourID . '/' . $vidID;
$path2 = $yourID . '/' . $vidID; // relative to wp-content/uploads
$prop_att_ids = (array) get_post_meta( $propertyID, 'imic_property_sights', false );
$to_copy = count( $prop_att_ids ); // test
$copied = 0; // test
foreach ( $prop_att_ids as $att_id ) {
// Check if we have a valid image/attachment.
if ( $att_id && $file = get_attached_file( $att_id ) ) {
$filename = basename( $file );
$file2 = $path . '/' . wp_unique_filename( $path, $filename );
// Copy the image file to $path.
if ( @copy( $file, $file2 ) ) {
// Copy the attachment (post) data.
$att = get_post( $att_id, ARRAY_A );
unset( $att['ID'] );
$att_id2 = wp_insert_attachment( $att, $file2 );
// Then add the meta data `vid_pix`.
add_post_meta( $vidID, 'vid_pix', $att_id2 );
// Copy the attachment's meta data. (no thumbnails)
$data = wp_get_attachment_metadata( $att_id );
$data['file'] = $path2 . '/' . basename( $file2 );
$data['sizes'] = [];
wp_update_attachment_metadata( $att_id2, $data );
$copied++; // test
}
} else {
$to_copy--; // test
}
}
echo $copied . ' image(s) out of ' . $to_copy . ' copied.<br>'; // test
var_dump( get_post_meta( $vidID, 'vid_pix', false ) ); // test
?>
PS: I previously misplaced the $to_copy--;
, though that's for testing only.