Wordpress Cron Jobs - 在设定时间后更改 post meta

Wordpres Cron Jobs - Changing post meta after set time

我无法在 wordpress 中执行 cron 作业。

设置: 我有一个自定义 post 类型 ('jobs'),它有几个元字段: 'featured' (bool) 如果有特色则设置为真 'feature-expiry' (DateTime) 这是功能过期的时间。

目标: 功能到期时间过后,更新 post 元并将 'featured' 更改为 false

我是 php 的新手,我似乎无法使用它。 cron 作业正常启动,但 posts 没有更新,我做错了什么?

/*           jj cron jobs              */

function jj_feaurecheck_cron_function( ) {

  global $post;

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

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

    $today = date( 'Ymd' );
    $expire = get_field( 'feature-expiry', false, false );
    $status = get_field( 'featured' );
        if ( $expire < $today ) :
            $status = 'false';
            update_field( 'featured', $status );
        endif;  
    endforeach;

}
if ( ! wp_next_scheduled( 'jj_feaurecheck_cron' ) ) {
    wp_schedule_event( date( 'Ymd' ), 'daily', 'jj_feaurecheck_cron' );
}
add_action( 'jj_feaurecheck_cron', 'jj_feaurecheck_cron_function' );

试试这个,我无法测试它,所以希望没有错误,但这是我用来做你正在尝试的事情的一般格式。

/**
 * jj cron jobs
 */
function jj_feaurecheck_cron_function( ) {

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

  $query = new WP_Query($args);

  if($query->have_posts()) {

    while($query->have_posts()) {

      the_post();

      //get the post id
      $post_id = get_the_ID();

      //get fields attached to post using ACF
      $expire = get_field( 'feature-expiry', $post_id, false );
      $status = get_field( 'featured', $post_id );

      //if not using ACF
      $expire = get_post_meta( $post_id, 'feature-expiry', true );
      $status = get_post_meta( $post_id, 'featured', true );

      //get current date
      $today = new DateTime();

      //convert expire into a date obj
      $expire_date = new DateTime($expire);

      //convert dates to seconds for easier comparison
      $expire_secs = $expire_date->format('U');
      $today_secs  = $today->format('U');

      //do the comparison
      if( $expire_secs < $today_secs ) {

          //update the field for that post ACF
          $status = 'false';
          update_field( 'featured', $status, $post_id );

          //non ACF
          update_post_meta($post_id, 'featured', $status);
      } 
    }
  }
}
add_action( 'jj_feaurecheck_cron', 'jj_feaurecheck_cron_function' );

[----修复----]

get_field()update_field() 用于我的项目未使用的高级自定义字段。切换到 get_post_meta()update_post_meta()

if ( $expire < $today ) 由于某种原因影响了设置为将来过期的帖子,所以我将它们切换到 unix 时间并且成功了。


/*           jj cron jobs              */

function jj_feaurecheck_cron_function( ) {

  global $post;

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

  $listings = get_posts( $args );
    foreach($listings as $post) : setup_postdata($post);
    
    $today = date( 'Ymd' );
    $expire = get_post_meta($post->ID, 'feature-expiry', true );
    $status = get_post_meta($post->ID, 'featured' );
    
      //get current date
      $today = new DateTime();

      //convert expire into a date obj
      $expire_date = new DateTime($expire);

      //convert dates to seconds for easier comparison
      $expire_secs = $expire_date->format('U');
      $today_secs  = $today->format('U');
    
      if ( $expire_secs < $today_secs ) :
          $status = 'false';
          //featured set to false
          update_post_meta($post->ID, 'featured', $status ); 
          //feature-expiry set back to empty
          update_post_meta($post->ID, 'feature-expiry', '' );
      endif;  
    endforeach;

}
add_action( 'jj_feaurecheck_cron', 'jj_feaurecheck_cron_function' );

感谢 disinfor 和 Steven 的指导,我无法用言语表达我的感激之情! =]