Code run fast on a cronjob but slow with a do_action or ajax call
I am creating several posts from a csv, when I execute the code using a cronjob the operation lasts about 7 minutes, but if I use the code directly in a template, or make a call to do_action or execute the code through ajax it takes more than 30 minutes and sometimes it doesn't even complete (generates a time limit error).
Does anyone know what could be happening?
For the creation of the posts I am using wp_insert_post, update_post_meta and wp_set_object_terms. Total posts created is 6544, when created WITHOUT the cronjob it tends to only create around 3000 +/- before failing
EDIT: Added the code used to create the posts
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;
}
}