我想做一个 cron 作业,女巫删除帖子自定义字段中所有早于日期的帖子。我的functions.php中有以下函数我的自定义字段名称是bewerbungs_frist
。
function foobar_truncate_posts(){
global $wpdb;
$currenttime = new DateTime();
$currenttime_string = $currenttime->format('Ymd');
# Set your threshold of max posts and post_type name
$post_type = 'job';
# Query post type
$query = "
SELECT ID FROM $wpdb->posts
WHERE post_type = '$post_type'
AND post_status = 'publish'
ORDER BY post_modified DESC
";
$results = $wpdb->get_results($query);
# Check if there are any results
if(count($results)){
foreach($results as $post){
$customfield = get_field('bewerbungs_frist', $post->ID);
$customfield_object = new DateTime($customfield);
$customfield_string = $customfield_object->format('Ymd');
if ( $customfield_string < $currenttime_string ) {
echo "The value of the custom date field is in the past";
echo $customfield_string;
$purge = wp_delete_post($post->ID);
}
}
}
}
foobar_truncate_posts();
我使用插件来处理我的 cronjobs。Hock 的名字是:foobar_truncate_posts
和 Arguments 是[]
cronjob 有效,但不会删除自定义字段日期早于今天日期的帖子。这两个变量是一样的。
$currenttime_string
20130820
$customfield_string
20130820