将附件复制到其他帖子类型并更改附件URL

时间:2018-10-11 作者:730wavy

在我的网站上,我必须自定义帖子类型和一个功能,让用户将照片从帖子类型a导入到帖子类型B,复制元数据,并将图像复制到为帖子类型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

?>
我现在面临的问题是,即使我将图像文件复制到了post type B的文件夹中,导入图像的附件url仍然与post type A相同。

**A -** /wp-content/uploads/2018/10
**B -** $path = \'/path/to/wp-content/uploads/\'.$yourID.\'/\'.$vidID;
使用上述功能导入图像时,如何更改附件url?

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

如果希望图像的URL不同,则需要先将图像文件复制到用户特定的文件夹,然后为复制的图像创建新附件&mdash;i、 e.克隆图像的“post”数据。

因此,请替换此:

// 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
使用此选项:

$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 );
        }
    }
}
请注意,由于我们正在创建一个新的附件帖子,我使用了wp_unique_filename() 为要复制到的图像文件生成唯一的文件名$path.

更新如果要生成所有缩略图,请替换此部分:

// 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 );
使用此选项:

// Create the attachment\'s meta data. (with thumbnails)
$att2 = get_post( $att_id2 );
wp_maybe_generate_attachment_metadata( $att2 );
以下是我使用的完整代码:

<?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:我之前把$to_copy--;, 虽然那只是测试用的

结束

相关推荐

Adding Images into API

我正试图将特色图片添加到我的API中,但这是一种不同类型的帖子。http://example.com/wp-json/wp/v2/directory我一直在使用WP REST API控制器,但它没有显示图像选项。我也尝试使用“更好的RESTAPI特色图片”,但运气不好。这开始让我恼火了。