我想在30天后垃圾(不強制刪除)自定義帖子類型。 要做到這一點,我發現從@彼得 - 古森一個很好的解決方案的天數後刪帖:https://wordpress.stackexchange.com/questions/209046/delete-expired-posts-after-a-number-of-days-after-they-expiredWordPress:垃圾30天后自定義帖子類型
我的問題是,該功能將刪除該自定義文章類型的所有帖子,並沒有按」不要使用垃圾。
我的代碼如下所示:
function get_exired_posts_to_delete()
{
/**
* If you need posts that expired more than a week ago, we would need to
* get the unix time stamp of the day a week ago. You can adjust the relative
* date and time formats as needed.
* @see http://php.net/manual/en/function.strtotime.php
* @see http://php.net/manual/en/datetime.formats.php
*/
// As example, we need to get posts that has expired more than 7days ago
$past = strtotime("- 1 week");
// Set our query arguments
$args = [
'fields' => 'ids', // Only get post ID's to improve performance
'post_type' => 'job',
'post_status' => 'publish',
'posts_per_page' => -1,
'date_query' => array(
'column' => 'post_date_gmt',
'before' => '30 days'
)
];
$q = get_posts($args);
// Check if we have posts to delete, if not, return false
if (!$q)
return false;
// OK, we have posts to delete, lets delete them
foreach ($q as $id)
wp_delete_post($id);
}
// expired_post_delete hook fires when the Cron is executed
add_action('expired_post_delete', 'get_exired_posts_to_delete');
// Add function to register event to wp
add_action('wp', 'register_daily_post_delete_event');
function register_daily_post_delete_event() {
// Make sure this event hasn't been scheduled
if(!wp_next_scheduled('expired_post_delete')) {
// Schedule the event
wp_schedule_event(time(), 'daily', 'expired_post_delete');
}
}
有幹啥錯日期查詢?
是否有更好的解決方案使用服務器cron而不是WP cron?