我从csv创建了几篇文章,当我使用cronjob执行代码时,操作会持续大约7分钟,但是如果我直接在模板中使用代码,或者调用do\\u action或通过ajax执行代码,则需要30分钟以上,有时甚至无法完成(生成时间限制错误)。
有人知道会发生什么吗?
对于帖子的创建,我使用wp\\u insert\\u post、update\\u post\\u meta和wp\\u set\\u object\\u terms。创建时,创建的帖子总数为6544篇WITHOUT 在失败之前,cronjob通常只创建3000+/-左右
编辑:添加用于创建帖子的代码
add_action(\'import_export_csv\', \'import_export_csv\');
function import_export_csv () {
try {
wp_defer_term_counting(true);
wp_defer_comment_counting(true);
// Read data from csv
$homeUrl = home_url();
$fileUrl = $homeUrl . \'/wp-content/uploads/csv/import_export.csv\';
$csv = array_map(\'utf8_encode\', file($fileUrl));
$csv = array_map(\'str_getcsv\', $csv);
$header = true;
$i = 0;
$dataArray = [];
$fullDataArray = [];
foreach ($csv as $key => $csvRow) {
// $csvSplit = explode(";", $csvRow[0]);
$i = 0;
$dataArray = [];
foreach ($csvRow as $value) {
if ($i == 0) {
$dataArray[\'flow\'] = $value;
}
if ($i == 1) {
$dataArray[\'Reporter\'] = $value;
}
if ($i == 2) {
$dataArray[\'Product\'] = $value;
}
if ($i == 3) {
$dataArray[\'HS-Code\'] = $value;
}
if ($i == 4) {
$dataArray[\'Unit\'] = $value;
}
if ($i > 4) {
if ($header) {
$dataArray[\'data\'] .= "\'" . $value . "\'" . \',\';
} else {
$resetNumCol = $i - 5;
$getYear = explode(\'-\', $columnYearValue[$resetNumCol]);
$getYear = str_replace("\'", "", $getYear);
$dataArray[\'date\'] = $columnYearValue[$resetNumCol];
$dataArray[\'year\'][$getYear[1]][\'data\'] .= $value . \',\';
$dataArray[\'data\'] .= $value . \',\';
}
}
$i++;
}
if ($header) {
$arrayHeader = $dataArray;
$header = false;
$columnYearValue = explode(",", $arrayHeader["data"]);
} else {
$arrayValue[] = $dataArray;
}
array_push($fullDataArray, $dataArray);
}
foreach ($fullDataArray as $productDatas) {
$productName = $productDatas[\'Product\'];
$productFlow = $productDatas[\'flow\'];
$productReporter = $productDatas[\'Reporter\'];
$productHSCode = $productDatas[\'HS-Code\'];
$productUnit = $productDatas[\'Unit\'];
$productYearDatas = $productDatas[\'year\'];
if ($productYearDatas) {
foreach ($productYearDatas as $key => $productYear) {
$productDate = \'20\' . $key;
$uniqueValuePost = $productDatas[\'Reporter\'] . \'-\' . $productName . \'-\' . $productDatas[\'HS-Code\'] . \'-\' . $productDatas[\'flow\'] . \'-20\' . $key;
$uniqueValuePost = sanitize_title($uniqueValuePost);
$productDataString = rtrim($productYear["data"], \',\');
$args = array(
\'posts_per_page\' => 1,
\'post_type\' => \'product\',
\'post_status\' => array(\'publish\', \'future\'),
\'name\' => wp_strip_all_tags($uniqueValuePost),
);
$product_posts = get_posts($args);
$generateDate = new DateTime(\'01/01/\' . $productDate);
$generateDateFormat = date_format($generateDate, \'Y-m-d H:i:s\');
if ($product_posts == NULL) {
$my_post = array(
\'post_title\' => wp_strip_all_tags($uniqueValuePost),
\'post_name\' => wp_strip_all_tags($uniqueValuePost),
\'post_date\' => $generateDateFormat,
\'post_content\' => \'\',
\'post_status\' => \'Publish\',
\'post_type\' => \'product\',
);
$postId = wp_insert_post($my_post);
update_post_meta($postId, \'unique_id\', $uniqueValuePost);
update_post_meta($postId, \'year_data\', $productDataString);
update_post_meta($postId, \'data_unit\', $productUnit);
wp_set_object_terms($postId, $productFlow, \'datatype\', false);
wp_set_object_terms($postId, $productReporter, \'country\', false);
wp_set_object_terms($postId, $productHSCode, \'hscode\', false);
wp_set_object_terms($postId, $productName, \'productname\', false);
} else {
update_post_meta($product_posts[0]->ID, \'year_data\', $productDataString);
update_post_meta($product_posts[0]->ID, \'data_unit\', $productUnit);
}
}
}
}
wp_defer_term_counting(false);
wp_defer_comment_counting(false);
echo "The file was uploaded and processed successfully";
} catch (Exception $e) {
wp_defer_term_counting(false);
wp_defer_comment_counting(false);
echo \'Error: \', $e->getMessage(), "\\n";
}
}