我有一个工作板自定义职位类型(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 );
}
}
}
(The
cvl_job_status_cron
事件正在运行,此函数
cvl_mark_as_expired
如Wp Cron Events插件所示连接到它)
最合适的回答,由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 );
}
}
}
}