WP_SET_POST_TERMS未随WP Cron事件更新

时间:2018-12-01 作者:richerimage

我有一个工作板自定义职位类型(job_listings) 具有分类法(cvl_job_status) 附有指定的各种标签,即:;Live、Fulled和Expired。

每个职位都有一个自定义字段(cvl_job_expires) 有到期日。

如果今天的日期大于保存的到期日期,我想使用WP Cron事件将分类法标记从Live更改为Expired。

首先,我看不出下面的代码有什么问题$post_ids 正在返回空数组。

有人能帮忙吗?

TIA公司

add_action(\'cvl_job_status_cron\', \'cvl_mark_as_expired\');

function cvl_mark_as_expired() {

  global $post;

  $post_ids = get_posts( 
    [
      \'post_type\' => \'job_listing\',
      \'posts_per_page\' => -1,
      \'no_found_rows\' => true,
      \'fields\' => \'ids\', //again, for performance
      \'tax_query\' => array(
        array(
          \'taxonomy\' => \'cvl_job_status\',
          \'field\' => \'id\',
          \'terms\' => 158, // ID of \'Live\' tag
        )
      )
    ] 
  );

  var_dump($post_ids); // this returns as empty??

  foreach($post_ids as $post_id) {

    $key = \'cvl_job_expires\'; // custom field name
    $expire_date = get_post_meta($post_id, $key, true); // Expiry Date saved as (d M y)
    $todays_date = date(\'d M y\'); // get todays date

    if ($expire_date < $todays_date) {

      $taxonomy = \'cvl_job_status\';
      $tag      = array( 159 );  // ID of \'Expired\' tag

      wp_set_post_terms( $post_id, $tag, $taxonomy );

      //  I have also tried this with
      //  wp_set_object_terms( $post_id, $tag, $taxonomy );

    }

  }

}
(Thecvl_job_status_cron 事件正在运行,此函数cvl_mark_as_expired 如Wp Cron Events插件所示连接到它)

1 个回复
最合适的回答,由SO网友:richerimage 整理而成

现在使用Wp查询-感谢评论中的Milo

add_action(\'cvl_job_status_cron\', \'cvl_mark_as_expired\');

function cvl_mark_as_expired() {

  global $post;

  $args = array(
    \'post_type\'              => array( \'job_listing\' ),
    \'posts_per_page\'         => \'-1\',
    \'fields\'                 => \'ids\',
    \'tax_query\'              => array(
      \'relation\' => \'AND\',
      array(
        \'taxonomy\'         => \'cvl_job_status\',
        \'terms\'            => \'live\',
        \'field\'            => \'slug\',
        \'include_children\' => false,
      ),
    ),
  );

// $post_ids = get_posts($args);

  $post_ids = new WP_Query( $args );

  if ( $post_ids->have_posts() ) {

    while ( $post_ids->have_posts() ) {

    $post_ids->the_post();

    $post_id = get_the_ID();

    $key          = \'cvl_job_expires\'; // custom field anme
    $expire_date  = get_post_meta($post_id, $key, true); // Now saved as Unix Timestamp
    $todays_date  = time(); // get todays date

      if ($expire_date < $todays_date) {

        $taxonomy = \'cvl_job_status\';
        $tag      = array( 159 );  // \'Expire\' tag id is 159

        wp_set_post_terms( $post_id, $tag, $taxonomy );

      }

    }

  }

}

相关推荐

显示所有类别而不是分配给特定帖子的类别的GET_TERMS

我的自定义帖子类型中有3篇帖子(\'careers\'). 我注册了自定义分类法(\'career_categories\').我创建了3个职位,其中两个职位属于“全职”,一个职位属于“兼职”。我在循环中使用以下代码:$terms = get_terms( \'career_categories\', array( \'hide_empty\' => true, ) ); $html_out .= \'<td class=\"column-\'. $col++ .\'\"&g