我正在尝试根据当前时间和之前的小时数(例如24小时)之间的比较自动删除某些帖子(自定义帖子类型)。自定义帖子类型为约会,如果未来的约会时间少于24小时,并且在meta\\u键“app\\u status”中保留meta\\u值“available”,我想将其删除。下面的代码是我正在尝试的,但它不起作用。我设置的app\\u date\\u开始时间小于24小时的帖子不会删除。我试过用不同的钩子把它钩住,但也没有成功。
function delete_unclaimed_apps () {
$avail = array(
\'post_type\' => \'appointment\',
\'meta_key\' => \'app_status\',
\'meta_value\' => \'available\',
);
$availapps = get_posts($avail);
$now = (time()-(7*60*60));
$cutoff = (get_field(\'minimum_reserve_notice\', \'options\') * 60 * 60);
foreach ($availapps as $post) {
setup_postdata( $post );
$appid = get_the_ID();
if (get_post_meta($appid, \'app_date_start\', true)) {
if (get_post_meta($appid, \'app_date_start\', true) < ($now + $cutoff)) {
wp_delete_post($appid, false);
}
}
wp_reset_postdata();
}
}
add_action(\'wp_enqueue_scripts\', \'delete_unclaimed_apps\');
最合适的回答,由SO网友:Eckstein 整理而成
我的结论是:基本上,我的计时逻辑是错误的。弄清楚时间和日期以及什么音符很难。。。
add_action(\'init\', \'delete_unclaimed_apps\');
function delete_unclaimed_apps() {
$availapps = get_posts(array(
\'post_type\' => \'appointment\',
\'post_status\' => \'publish\',
\'meta_query\' => array (
array(
\'key\' => \'app_status\',
\'value\' => \'available\',
),
),
));
foreach ($availapps as $post) {
$timestamp = get_post_meta($post->ID, \'app_timestamp\', true);
$now = time() - (7*60*60);
$cutoff = (get_field(\'minimum_reserve_notice\', \'options\') * 60 * 60);
if ($now > ($timestamp - $cutoff)) {
wp_trash_post($post->ID);
}
}
}