1

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";
    }
} 
3
  • there's no code in your question to debug, can you edit it to include all the information needed to answer the question? Use the edit link under the tags
    – Tom J Nowell
    Aug 22 '21 at 0:47
  • @TomJNowell hi, yeah sure, added the code
    – Kleiver
    Aug 22 '21 at 1:38
  • 1
    and you're sure cron jobs run on the same server with the same priority using the same method? If you're on managed hosting they may run in slightly different environments and priorities, e.g. it may not run faster at all, but rather your host may use CLI or an actual cronjob to trigger WP Cron avoiding the time limit. 30 minutes is an extremely large time limit, if you had said 30 seconds that would be more typical
    – Tom J Nowell
    Aug 22 '21 at 22:40
0

In cPanel run hosting environments you might have one PHP version used for your web applications but another one used for WP-CLI.

Checking the PHP version, used by WP-CLI is done by running wp --info. You can check your PHP version in WordPress by going to the Health Check status page. It should be warning you anyway, if you are using an older version than 7.4.

Another reason that might be slowing you down is the presense of hooks and filters, related to operations with an active user session or being done in wp-admin. When you run a WP-CLI command, you don't have an active user session (is_user_logged_in() returns false) and operation is not considered as run in wp-admin (is_admin() returns false).

Yet another reason - plugins checking for the presense and value of WP_CLI constant could be excluding their operations from WP-CLI, thus burdening the process when you are doing the import through the web interface.

This is not "the ultimate" list of reasons, but definitely some that I would check before digging deeper into profiling the WP-CLI execution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.