【问题标题】:Update custom field with Cron Job in WordPress在 WordPress 中使用 Cron Job 更新自定义字段
【发布时间】:2016-11-30 02:31:06
【问题描述】:

我正在尝试为自定义帖子类型(职位列表)设置一个 cron 作业,其中包含两个 ACF 自定义字段。

  1. Datepicker - 用户可以选择工作的结束日期('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' );
    }
}

【问题讨论】:

    标签: wordpress cron custom-post-type advanced-custom-fields


    【解决方案1】:

    我最终重写了大部分代码,这是有效的:

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

    【讨论】:

    • 我知道这是一篇很老的帖子,但wp_schedule_event 中的第一个参数接受时间戳,而不是您所拥有的Ymd 格式的日期。
    【解决方案2】:

    试试这个:

        $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' )

    【讨论】:

    • 还使用 DATE 进行了测试,并检查了日期格式是否匹配。我还创建了一个自定义页面模板并运行此 WP_Query 以查看它是否回显了正确的详细信息,它确实如此。
    猜你喜欢
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    相关资源
    最近更新 更多