2016-07-26 22 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 필드를 내가 코딩 한 파일에서 사용할 수 없습니까?

$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'); 
    } 
} 
관련 문제