2016-07-26 13 views
0

2つの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フィールドが使用できないことがありますか?

$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

これを試してみてください

// 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'); 
+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() { 

    // 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'); 
    } 
} 
関連する問題