0

投稿のカスタム フィールドの日付より古いすべての投稿を削除する 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_string20130820 $customfield_string20130820

4

1 に答える 1

1

コードにタイプミスがあります。 の末尾に「s」がありません$result

これ:

  foreach($result as $post){

これでなければなりません:

  foreach($results as $post){

試してみました。その修正を行うと、 wp_delete_post() はうまく機能します。


編集


あなたが何をしようとしているのか本当にわかりません。カスタムフィールドが過去のある時点に設定されているかどうかを確認したいですか? その目的は何continueですか?また、高度なカスタム フィールド (ACF) を使用していると思います。jquery Date Picker を使用すると、日付をYmdデフォルトでフォーマットできるため、DateTime オブジェクトに変換する必要はありません。

とにかく、この関数は時間値を適切に設定して比較する方法を説明する必要があり、そこから取得できるはずです。

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 = '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";

                    $purge = wp_delete_post($post->ID);
                }
            }
        }

}

foobar_truncate_posts();
于 2013-08-18T00:27:52.337 に答える