0

我已经在我的 WordPress 自定义插件中添加了这个 cronjob,但是当我手动运行该功能时,它正在工作,但通过 cronjob,它不工作。这是我的代码!所有插件文件均已正确加载,未显示任何错误。谁能帮我解决这个问题!我已经尝试了很多,但仍然无法正常工作。

    <?php

         if (!wp_next_scheduled('generateusedcarsfeed'))
        {
            wp_schedule_event(time() , '5mins', 'generateusedcarsfeed');
        }

        add_action('generateusedcarsfeed', 'generate_used_car_feed');
if (!wp_next_scheduled('generateusedcarsfeed'))
{
    wp_schedule_event(time() , '5mins', 'generateusedcarsfeed');
}

add_action('generateusedcarsfeed', 'generate_used_car_feed');

// Getting all used cars with all data

function generate_used_car_feed(){
    try{
        // for opening the csv file
        $file = fopen('all-cars-feed.csv', 'r');
        fwrite($file, '');
        $loop = new WP_Query($args);
        chmod('all-cars-feed.csv', 0777);

        // for creating array from csv file
        $file="all-cars-feed.csv";
        $csv= file_get_contents($file);
        $array = array_map("str_getcsv", explode("\n", $csv));
        $json_csv_arr = json_encode($array);
        $my_array_csv = json_decode($json_csv_arr, true);

        // for getting used car list from databse, only take publish used car
        global $wpdb;
        $query = "SELECT wp_postmeta.post_id, wp_postmeta.meta_value FROM wp_postmeta 
        INNER JOIN wp_posts ON wp_posts.id = wp_postmeta.post_id WHERE meta_key = 'car_registration_number' 
        AND post_status = 'publish'";
        $result = $wpdb->get_results($query);
        $json_reg_num = json_encode($result);
        $my_array = json_decode($json_reg_num, true);
        print_r($my_array); 
        foreach ($my_array as $value) {
            $car_reg_array = $value['meta_value'];
            $Post_id= $value['post_id'];
            $isTrue=true;

            $ch = fopen("all-cars-feed.csv", "r");
            while($row = fgetcsv($ch)) {
            if (in_array($car_reg_array, $row)) {
                echo 'found</hr>';
                $isTrue=false;
            }
         }
        if($isTrue)
        {
            $wpdb->query(
              'UPDATE   '.$wpdb->prefix.'posts SET post_status = "trash"
               WHERE ID = "'.$Post_id.'"');
        }
}

        //fclose($file);
    }
    catch(\Exception $e)
    {
        $txt = 'Message: ' . $e->getMessage();
        $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");

        fwrite($fileLoger, $txt);
        fclose($fileLoger);
    }
}
4

1 回答 1

0

我想知道这个5mins间隔是否真的存在于您的设置中。尝试添加此代码以确保它确实存在(如果它已经存在,它不会破坏东西):

function add_custom_cron_schedules($schedules){
  if (!isset($schedules["5mins"])) {
    $schedules["5min"] = array(
      'interval' => 5*60,
      'display' => __('Every 5 minutes'));
  }
  return $schedules;
}
add_filter('cron_schedules','add_custom_cron_schedules');

如果您想检索当前可用的时间表,您还可以使用wp_get_schedules()并检查某个键(如“5mins”)是否存在。

于 2020-03-16T10:21:24.473 回答