copy attachments to another post type and change attachment url

On my site I have to custom post types and a function that let’s users import photos from post type A to post type B, copying the meta values and it copy’s the image to the folder specified for post type B —

<?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 = ‘/path/to/wp-content/uploads/’.$yourID.’/’.$vidID;

// 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 ) {
add_post_meta( $vidID, ‘vid_pix’, $att_id );
$filename = basename ( get_attached_file( $att_id ));
$url = wp_get_attachment_url( $att_id ) : ”;
copy($url, $path.’/’.$filename.’.png’);
//echo $url;
}

$vid_pix2 = get_post_meta( $vidID, ‘vid_pix’, false ); // test
//var_dump( $vid_pix, $vid_pix2, $url ); // test

?>

The issue Im having now is that the attachment url still for the imported images are still the same as post type A even though I copied the image files over to post type B’s folder.

**A -** /wp-content/uploads/2018/10
**B -** $path = ‘/path/to/wp-content/uploads/’.$yourID.’/’.$vidID;

How can I change the attachment url when importing the images using my above function?

Read more here:: copy attachments to another post type and change attachment url

Leave a Reply

Your email address will not be published. Required fields are marked *