copy attachments to another post type and change attachment url

by Rich   Last Updated October 11, 2018 15:08 PM - source

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 ---

// 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?

Related Questions

update post meta: Insert attachment to existing field

Updated October 09, 2017 09:08 AM

ajax delete value from custom field array

Updated June 30, 2018 19:08 PM

How to get custom field image url of specific size

Updated November 14, 2016 07:32 AM