php - 使用 wp_insert_post() 添加批量帖子获取内部服务器错误
问题描述
我编写了一个批量导入脚本,在其中wp_insert_post()
使用分类名称导入带有函数的帖子。基本上有4000个城市,我想将它们添加为帖子,例如:
- 纽约到伦敦
- 纽约到阿姆斯特丹
- 纽约到柏林
ETC...
但是在添加了几千个帖子后,我遇到了内部服务器错误。我预计添加的帖子总数为 16,000,000 [4,000 * 4,000]
这是代码:
<?php
require_once "wp-load.php";
ini_set("memory_limit",-1);
set_time_limit(0);
ignore_user_abort(true);
$term_query = new WP_Term_Query( array( 'taxonomy' => 'cities' ) );
if ( ! empty( $term_query->terms ) ) {
wp_defer_term_counting( true );
define( 'WP_IMPORTING', true );
foreach ( $term_query ->terms as $term ) {
$city1 = $term->name;
foreach ( $term_query ->terms as $inner_city ) {
$city2 = $inner_city->name;
$postTitle = $city1 . ' to ' . $city2;
$postSlug = $city1 . '-to-' . $city2;
$LowerPostSlug = strtolower($postSlug);
$post_args = array(
'post_title' => $postTitle,
'post_status' => 'publish',
'post_name' => $postSlug,
'post_author' => $user_ID,
'post_type' => 'conversion',
);
$found_post_title = get_page_by_title( $postTitle, OBJECT, 'city_post' );
$found_post_id = $found_post_title->ID;
if ( FALSE === get_post_status( $found_post_id ) ):
$returned_post_id = wp_insert_post( $post_args );
update_field( 'city_1', $city1, $returned_post_id ); # SET CUSTOM FIELD FOR city 1
update_field( 'city_2', $city2, $returned_post_id ); # SET CUSTOM FIELD FOR city 2
echo $postTitle . '</br>';
endif;
}
}
wp_defer_term_counting( false );
}
解决方案
推荐阅读
- spring-boot - 没有符合条件的 bean 类型 - Kotlin Spring boot
- regex - 如何使用正则表达式和记事本 ++ 中的替换对话框复制和粘贴选定的文本?
- google-apps-script - 使用循环复制到新工作表后,获取工作表名称无法正常工作或无法自动更新
- google-apps-script - 如何制作一个忽略已用油漆桶填充的奇数单元格的条件格式的谷歌电子表格公式?
- awk - 在 awk 中处理之前向当前记录添加一个字段
- php - 同时或同时将 lastInsertId 从一个表插入到另一个表的可能方法
- javascript - 命令完成时间过长
- android - bundle.js 文件在文件已经在 pwa 的缓存中后被删除
- c - 查找两个用户输入数组的均值、众数和中位数的 C 代码
- python - python yield/send如何做re-entry