如果其他人需要该代码,我就是这样解决的@pieter goosen是对的,我确实需要查询正确的帖子,但真正的诀窍是在运行帖子查询时匹配jquery datepicker中的日期格式。检查下面的日期格式,jquery是“yy-mm-dd”,php等效的是“Y-m-d”。
所以datepicker的js是:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(\'.ExDate\').datepicker({
dateFormat: \'yy-mm-dd\'
});
});
</script>
cron函数是:
add_action( \'wp\', \'delete_expired_adverts_daily\' );
function delete_expired_adverts_daily() {
if ( ! wp_next_scheduled( \'delete_expired_adverts\' ) ) {
wp_schedule_event( time(), \'daily\', \'delete_expired_adverts\');
}
}
add_action( \'delete_expired_adverts\', \'delete_expired_adverts_callback\' );
function delete_expired_adverts_callback() {
$args = array (
\'post_type\' => \'advert\',
\'meta_key\' => \'expires\',
\'meta_value\' => date(\'Y-m-d\'),
\'meta_compare\' => \'<=\',
);
$query_ads = new WP_Query($args);
if ($query_ads->have_posts()):
while($query_ads->have_posts()): $query_ads->the_post();
wp_delete_post(get_the_ID(), true);
endwhile;
endif;
}