如何根据自定义字段自动删除wordpress自定义帖子

时间:2017-07-19 07:06:48

标签: php wordpress

当活动日期超过一周前,我想自动将议程项目(自定义帖子)放入垃圾箱。这个函数(在functions.php中)似乎部分工作,有时也会破坏较新的事件。并且它在发布新事件时会产生问题,因为它在发布之前已经在垃圾箱中。

    function trash_old_events(){

    global $wpdb;

    $lastweek = strtotime( "-7 days" );
    $lastweek = date( "Ymd", $lastweek );

    $query = "
        SELECT ID FROM $wpdb->posts
        WHERE post_type = 'agenda' OR post_type = 'members_agenda'
        AND post_status = 'publish'
    ";

    $results = $wpdb->get_results($query);

    if(count($results)){
            foreach($results as $post){

                $startdate = get_field('agenda_date', $post->ID);
                $startdate = strtotime( $startdate );
                $startdate = date( "Ymd", $startdate );

                if ( $startdate < $lastweek ) {
                    wp_trash_post($post->ID);
                }
            }
        }
}
trash_old_events();

0 个答案:

没有答案