我有一个函数,可以在特定时间后删除自定义帖子类型。就我而言,30天后。问题是,这些帖子并没有被丢弃。相反,WordPress使用新ID创建帖子的新版本,并将此新版本移动到垃圾箱中。旧版本的状态仍为“已发布”。
我用删除帖子wp_trash_post()
. 我尝试使用永久删除它们wp_delete_post()
. 问题是,wp\\u delete\\u post只适用于本机post类型,而不适用于自定义post类型。
还有一些奇怪的事情。。。这个函数在一段时间内工作得很好,现在它坏了?!新的WordPress版本(4.9)中是否可能存在bug?
以下是我的函数代码:
<?php
function get_delete_old_jobs() {
// WP_Query arguments
$args = array(
\'fields\' => \'ids\', // Only get post ID\'s to improve performance
\'post_type\' => array( \'post_type\' ), //post type
\'posts_per_page\' => \'-1\',//fetch all posts,
\'date_query\' => array(
\'before\' => date(\'Y-m-d\', strtotime(\'-30 days\'))
)
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$postid = get_the_ID();
$post = get_post($postid);
// do something
// ....
// here I generate an e-mail for the author
wp_mail($author->user_email, $subject, $message, $headers);
wp_trash_post( $id );
// also tried this version
// wp_trash_post( $post_id );
}
} else {
// no posts found
return false;
}
// Restore original Post Data
wp_reset_postdata();
}
// expired_post_delete hook fires when the Cron is executed
add_action( \'old_job_delete\', \'get_delete_old_jobs\' );
// Add function to register event to wp
add_action( \'wp\', \'register_daily_jobs_delete_event\');
function register_daily_jobs_delete_event() {
// Make sure this event hasn\'t been scheduled
if( !wp_next_scheduled( \'old_job_delete\' ) ) {
// Schedule the event
wp_schedule_event( time(), \'hourly\', \'old_job_delete\' );
}
}
?>