2016-07-26 38 views
0

我想爲包含兩個ACF自定義字段的自定義帖子類型(作業列表)設置一個cron作業。用WordPress中的Cron作業更新自定義字段

  1. 日期選擇 - 用戶可以選擇關閉作業(「job_listing_closing_date」)
  2. 無線電場的日期 - 開閉&選擇。 (「job_listing_status」)

我所需要的射頻場,如果job_listing_closing_date已通過後端後編輯屏幕從「打開」更改爲「關閉」。這是我目前的代碼,位於'/wp-content/themes/themename/assets/functions/cpt-job-listings.php文件中。

我已將下面的代碼添加到網站,但沒有任何反應。 也許查詢錯誤或ACF字段在我編碼的文件中不可用?

// Create a cron job in order to check the custom field of 'job_listing_closing_date' against today's date. If the date has passed, set the job status to 'closed' and display different content on front-end. 

// Scheduled Action Hook 
function check_job_end_date() { 

    // WP_Query arguments 
    $listings = array (
     'post_type'    => 'job_listings', 
     'posts_per_page'   => -1, 
     'meta_key'    => 'job_listing_closing_date', 
     'meta_query' => array(
      'key'  => 'job_listing_closing_date',  
      'value' => date('Ymd'), 
      'compare' => '<', 
      'type' => 'NUMERIC', 
     ) 
    ); 

    global $post; 
    if ($listings->have_posts()) { 
    while ($listings->have_post()) { 
     $listings->the_post(); 
     update_field('job_listing_job_status', 'Closed'); 
     //update_post_meta($post->ID, 'job_listing_job_status', 'Closed'); 
    } 
    wp_reset_postdata(); 
    } 
} 

// Schedule Cron Job Event 
function job_listing_cron_job() { 
    if (! wp_next_scheduled('check_job_end_date')) { 
     wp_schedule_event(date('Ymd'), 'daily', 'check_job_end_date'); 
    } 
} 

回答

0

試試這個:

$listings = array (
    'post_type'    => 'job_listings', 
    'posts_per_page'   => -1, 
    'meta_key'    => 'job_listing_closing_date', 
    'meta_query' => array(
     'key'  => 'job_listing_closing_date',  
     'value' => date('Ymd'), 
     'compare' => '<', 
     'type' => 'DATE', 
    ) 
); 

而且肯定是正確的日期格式date('Ymd')

+0

測試與DATE也檢查日期格式匹配。我還創建了一個自定義頁面模板並運行這個WP_Query,以查看它是否回顯了正確的細節,它是幹什麼的。 –

0

我結束了重寫大部分的代碼,這是什麼工作:

// Create a cron job in order to check the custom field of 'job_listing_closing_date' against today's date. If the date has passed, set the job status to 'closed' and display different content on front-end. 

// Scheduled Action Hook 
function check_job_end_date() { 

    global $post; 

    $args = array( 
    'post_type'  => 'job_listings', 
    'posts_per_page' => -1, 
); 

    $listings = get_posts($args); 
    foreach($listings as $post) : setup_postdata($post); 

    $today = date('Ymd'); 
    $expire = get_field('job_listing_closing_date', false, false); 
    $status = get_field('job_listing_job_status'); 
     if ($expire < $today) : 
      $status = 'Closed'; 
      update_field('job_listing_job_status', $status); 
     endif; 
    endforeach; 

} 

// Schedule Cron Job Event 

if (! wp_next_scheduled('job_listing_cron_job')) { 
    wp_schedule_event(date('Ymd'), 'daily', 'job_listing_cron_job'); 
} 
add_action('job_listing_cron_job', 'check_job_end_date');