我想知道是否可以加快我为 wordpress 网站创建的现有导入过程?我需要从外部 xml 文件导入数据,根据该数据创建产品,然后下载图像并将它们分配给创建的产品。问题是缩略图还需要通过 wp 函数生成并关联到产品。我目前设法每秒插入 1 到 2 张图像,使用 cron 作业每 5 分钟调用一次脚本,导入限制为 120 张图像。对于 120 张图像的 block ,这相当于大约 80 到 150 秒。 Tre 问题是我需要导入大约 10000 个产品和 200000 张图片。
在 tmp 文件夹中每次大约有 1000 张图像
我目前使用的功能是:
function upload_image(){
ini_set("memory_limit","2048M");
add_filter('intermediate_image_sizes', function ($image_sizes){
return array('thumbnail');
},1000 );
if($this->ids===false) {
self::get_all_ids();
}
$upload_dir = wp_upload_dir();
$root = $upload_dir['basedir'].DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR;
$handle = opendir($root);
$files=array();
$propertys=array();
$propertys_images=array();
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$tmp_data=explode('_',$entry);
if(isset($tmp_data[1])) {
$post_id = isset($this->ids[$tmp_data[1]]) ? $this->ids[$tmp_data[1]] : 0;
if (!empty($post_id)) {
$propertys[$post_id] = $post_id;
$files[] = array('url' => $root . $entry, 'post_id' => $post_id, 'name' => $entry);
}else{
unlink( $root . $entry);
}
}else{
unlink( $root . $entry);
}
}
}
if (empty($propertys)){return'';}
global $wpdb;
$results = $wpdb->get_results("
SELECT ID,post_parent, post_title
FROM $wpdb->posts AS posts
WHERE post_type = 'attachment'
AND post_parent IN(".implode(',',$propertys).")
Order by ID asc
",ARRAY_A ) ;
$set_images=array();
foreach ($results as $r){
$set_images[]=$r['post_title'];
$propertys_images[$r['post_parent']][$r['ID']]=$r['ID'];
}
foreach ($files as $k => $d){
if(in_array($d['name'],$set_images)){
unset($files[$k]);
unlink($d['url']);
}
}
require_once( ABSPATH . 'wp-admin/includes/image.php' );
foreach ($files as $id_of_i => $f){
if($id_of_i > 120){continue;}
$image_url = $f['name'];
$post_id=$f['post_id'];
$base_path=$upload_dir['basedir'].DIRECTORY_SEPARATOR.'tmp' .DIRECTORY_SEPARATOR.$image_url;
$filename = basename( $image_url );
if ( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
}
else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
if(file_exists($base_path)) {
rename($base_path, $file);
//copy($base_path, $file);
}
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_parent' => $post_id,
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file );
$propertys_images[$post_id][$attach_id]=$attach_id;
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
foreach ($propertys_images as $prop_id => $images){
$images_ids=array_values($images);
set_post_thumbnail($prop_id, $images_ids[0]);
update_post_meta($prop_id, '_thumbnail_id', $images_ids[0]);
update_post_meta($prop_id, 'fave_prop_slider_image', $images_ids[0]);
$num=get_post_meta($prop_id,'numb_of_images',true);
delete_post_meta($prop_id,'fave_property_images');
foreach ($images_ids as $k =>$id){
if($k!='0'){
add_post_meta($prop_id, 'fave_property_images', $id);
}
}
if($num<=count($images_ids)){
update_post_meta($prop_id, 'all_images_imp', 'true');
}
}
exit();
}
首先我设置只生成这个图像的缩略图, 然后我收集所有产品代码(“get_all_ids()”) 然后我从 tmp 文件夹中获取所有图像并检查该 img 的产品是否存在, 然后获取我拥有其图像的产品的所有附件 然后我将图像迁移到 wp 上传路径
然后调用“wp_insert_attachment”添加基础图像, 然后使用“wp_generate_attachment_metadata”我生成一个缩略图 然后我使用“wp_update_attachment_metadata”将这些信息附加到附件
收到我添加到产品和产品库的附件 ID
我认为在这个脚本中减慢的部分是生成缩略图 有没有可能加快这个过程,或者如果你有什么建议
以目前的速度,添加所有图像大约需要 5 到 10 天
提前致谢
最佳答案
问题出在 EWWW Image Optimizer 所以你需要注意你的插件之间是否有某种优化器处于事件状态。 当我禁用插件(断开 Hook )时,插入图像的速度增加了战利品
关于php - WordPress 从服务器导入大量图片并与帖子关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53013083/