【问题标题】:Delete posts with custom field date older than current date via cronjob通过 cronjob 删除自定义字段日期早于当前日期的帖子
【发布时间】:2013-08-17 23:28:52
【问题描述】:

我想做一个 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,参数是 []

cronjob 有效,但不会删除自定义字段日期早于今天日期的帖子。这两个变量是一样的。

$currenttime_string20130820 $customfield_string20130820

【问题讨论】:

    标签: wordpress custom-fields cron-task


    【解决方案1】:

    您的代码中有错字,$result 末尾缺少一个“s”。

    这个:

      foreach($result as $post){
    

    应该是这样的:

      foreach($results as $post){
    

    我刚刚试了一下。一旦你解决了这个问题, wp_delete_post() 就会很好用。


    编辑


    我真的不清楚你想做什么。您想检查自定义字段是否设置为过去的某个时间? continue 的用途是什么?另外,我猜您正在使用高级自定义字段 (ACF)。使用 jquery 日期选择器,您可以默认将日期格式设置为 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();
    

    【讨论】:

    • 它对我不起作用。您是否尝试过使用自定义帖子类型和日期自定义字段?
    • 另一个问题是您的“小于”比较试图将对象与字符串进行比较,因此它会使您的函数崩溃。 $currenttime 必须像这样转换:$currenttime_string = $currenttime-&gt;format('Ymd');
    • $currenttime_string 和 $customfield_string 具有相同的值。今天是 2013 年 8 月 20 日,如果我发布一些自定义字段(ACF 插件)设置为 19.08.2013 的内容,cronjob 不会删除这篇文章。
    • 当你离开时会发生什么:echo $customfield?您的自定义字段可能设置错误。
    猜你喜欢
    • 2013-02-09
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    相关资源
    最近更新 更多