投稿のカスタム フィールドの日付より古いすべての投稿を削除する 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();
プラグインを使用して cron ジョブを処理します。Hock 名は:foobar_truncate_posts
であり、Arguments は[]
cron ジョブは機能しますが、カスタムフィールドの日付が今日の日付よりも古い投稿は削除されません。2 つの変数は同じです。
$currenttime_string
20130820
$customfield_string
20130820