copy images from custom field to another custom field

On my wordpress site I have a front end form that allows users to create posts and upload photos for a custom post type ‘Property’.

Using the following function –

$property_sights_value = get_post_meta($Property_Id, ‘imic_property_sights’, false);

if (!empty($_FILES[‘sightMulti’][‘tmp_name’][0])) {
$i = 1;
$files = $_FILES[‘sightMulti’];
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]
);
$_FILES = array(“sight” . $i => $file);
$newuploadMulti = sight(“sight” . $i, $pid);
if ($i == 1) {
update_post_meta($pid, ‘_thumbnail_id’, $newuploadMulti);

}
add_post_meta($pid, ‘imic_property_sights’, $newuploadMulti, false);
}
$i++;
}
}
}

And then on another page I have the same thing except for a different custom post type ‘v_editor’, and a different upload function, which converts images to .png format and gets rid of the original –

$vid_pix = get_post_meta($v_Id, ‘vid_pix’, false);

if (!empty($_FILES[‘vidPix’][‘tmp_name’][0])) {
$i = 1;
$files = $_FILES[‘vidPix’];
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]
);
$_FILES = array(“sight” . $i => $file);
add_filter( ‘upload_dir’, ‘wpse_141088_upload_dir’ );
add_filter(‘intermediate_image_sizes_advanced’, ‘no_image_resizing’);

$mfile = wp_handle_upload($files, $upload_overrides );

$newvidPix = sight(“sight” . $i, $v_Id);
remove_filter( ‘upload_dir’, ‘wpse_141088_upload_dir’ );
remove_filter(‘intermediate_image_sizes_advanced’, ‘no_image_resizing’);
// Convert the image to PNG and delete the old image.
attachment_to_png( $newvidPix );

if ($i == 1) {
update_post_meta($v_Id, ‘_thumbnail_id’, $newvidPix);
}
add_post_meta($v_Id, ‘vid_pix’, $newvidPix, false);
}
$i++;
}
}

Both functions work fine, however, what I am trying to do is–

On the ‘v_editor’ cpt form, I am trying to create a select dropdown to let the user choose a post of his from the ‘property’ cpt, and grab the images saved there to copy over to use. —

<?php query_posts(array(‘post_type’=>’property’,’order’=>’desc’,’author’=>$current_user->ID,’posts_per_page’ => 25,’paged’ => get_query_var(‘paged’),’post_status’ => array(‘publish’,’pending’,’draft’)));
if(have_posts()):

echo ‘<select id=”property_posts” name=”property_posts”>’;

while(have_posts()):the_post();

echo ‘<option value=”‘.get_the_ID().'”>’.get_the_title().'</option>’;

endwhile;

echo ‘</select>’;

endif; ?>

<button type=”button” id=”import_pics”>Import</button>

Both of the images when uploaded get stored to meta keys –

PROPERTY CPT – $property_sights_value = get_post_meta($Property_Id, ‘imic_property_sights’, false);

V_EDITOR CPT – $vid_pix = get_post_meta($v_Id, ‘vid_pix’, false);

How can I achieve this?

Read more here:: copy images from custom field to another custom field

Leave a Reply

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