【发布时间】:2016-11-30 02:31:06
【问题描述】:
我正在尝试为自定义帖子类型(职位列表)设置一个 cron 作业,其中包含两个 ACF 自定义字段。
- Datepicker - 用户可以选择工作的结束日期('job_listing_closing_date')
- 单选字段 – 打开和关闭选项。 (‘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' );
}
}
【问题讨论】:
标签: wordpress cron custom-post-type advanced-custom-fields