基本上,我编写了一个函数,可以根据Posteta表中的字段将post状态更改为draft:
/**
* Remove ads if they have been sold for over 5 days
*/
function cp_remove_sold_ads(){
global $wpdb;
// Get all sold ads
$sold_ads = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "postmeta WHERE `meta_key` = \'cp_ad_sold_date\' AND `meta_value` <> \'\'");
foreach ($sold_ads as $ad) {
$today = time();
// Get day, month, year
$date = explode(\'-\',get_post_meta($ad->post_id, \'cp_ad_sold_date\', true));
$sold_date = mktime(null, null, null, $date[1], $date[2], $date[0]);
$date_diff = $today - $sold_date;
// Get the days difference
$sold_day_diff = floor($date_diff / (60*60*24));
if ($sold_day_diff >= 5) {
wp_update_post(array(\'ID\' => $ad->post_id, \'post_status\' => \'draft\'));
}
}
}
这很好,如果我将该函数添加到init操作中,它会执行它应该执行的操作:
add_action( \'init\' , \'cp_remove_sold_ads\' );
然而,我想让这个操作每天都执行,我四处看看,发现WP使用WP\\U schedule\\u事件来处理hanlde cron作业,但我不知道如何使用它,有人知道我需要添加什么来处理它吗?
提前谢谢!