是否可以让 WP 识别添加(过去)年/月目录到 /uploads?

问题描述 投票:0回答:1

我有 50,000 个图像文件要上传,并希望将它们分段到我添加到 /uploads 的目录中,这些目录与 WordPress 的年/月目录命名和结构相匹配:

2010年 -01 -02 -03 ...

这些月份和年份不会显示在媒体库“所有日期”过滤器中,因为在添加的实际月份和年份目录期间尚未上传任何文件(通过媒体库上传)。

我已将测试图像添加到每个月目录中,然后尝试各种插件将其注册到媒体库。我想我记得它最近使用“从服务器添加”插件工作,但我无法重现它。我尝试过重新生成缩略图插件、批量媒体注册插件、媒体同步插件以及如上所述的从服务器添加插件。我还通过 WP-CLI 尝试过“wp media regenerate --yes”。

问题似乎是添加的图像没有附件,所以WP没有注册目录。

我希望将年/月目录注册到媒体库,以便我可以按“所有日期”进行过滤(无需使用媒体库文件夹 Pro 等插件添加不同的媒体库)。

如果没有真正的技术理解,我认为如果将 50,000 张图像(在 WP 生成所有 WP、Woocommerce 和主题缩略图后将变成大约 500,000 张图像)分割成 300 个目录,而不是分割成 300 个目录,数据库的服务速度会更快只有一个。想法?

感谢您的任何建议。

wordpress plugins directory upload media
1个回答
0
投票

这是用于将图像添加到 WordPress 媒体库的 php 代码。您需要设置一个方案来运行批量图像,因为这需要对每个图像执行。

$file_name = 'file_name'; // get the file name and assign it to this variable
    $date_folder_path = '/2024/01/'; // assign the correct path
// the file_name and folder path must lead to the correct image file inside the wp_uploads folder
    $publish_date = '2024-01-15  00:00:00';  //Assign a date having the year and month you want this to appear in the Media Library

// with these values set, your work is done and the following lines will add the image to the Media Library
                
$attachment = array();
$attachment['post_title'] = $file_name;
$attachment['post_content'] = '';
$attachment['post_status'] = 'publish';
$attachment['post_date'] = $publish_date;
$filename = $_SERVER['DOCUMENT_ROOT'].'/wp-content/uploads/'.$date_folder_path.'.$file_name;
$attachment['post_mime_type'] = mime_content_type( $filename );
$media = wp_insert_attachment( $attachment, $filename, 0 ); // the 0 means you aren't attaching it to any particular post
$attach_data = wp_generate_attachment_metadata( $media, $filename ); 
wp_update_attachment_metadata( $media, $attach_data );
© www.soinside.com 2019 - 2024. All rights reserved.