douwu8251 2019-01-28 20:55
浏览 65

尝试根据datepicker值将wp post设置为draft模式

I have a custom post type called coupons. All coupons contain an ACF field called 'expiration_date' which is a datepicker field. I am trying to use this field to set the post status to 'draft' after the expiration date passes. I'm not sure if I'm just not using the correct wp hook or if my code is incorrect, but its not working. Could anyone offer a suggestion? Many thanks in advance.

<?php add_action( 'save_post', 'remove_expired_coupons', 10, 2 );

function remove_expired_coupons( $post_ID, $post )
{
$expiry = new DateTime();
$expiry->setTime(0,0,0);
$expiresValue = $expiry->format('Y-m-d');

    $args = array(
        'post_type' => 'coupon',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'meta_query' => array(
            array(
                'key' => 'expiration_date',
                'value' => $expiresValue,
                'compare' => '<=',
                'type' => 'DATE'
            ),
        ),
    );

$posts = get_posts($args);
foreach( $posts as $post ) {
    if(get_field('expiration_date', $post->ID)) {
        $postdata = array(
            'ID' => $post->ID,
            'post_status' => 'draft'
        );
        wp_update_post($postdata);
    }
}
} 
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 怎么让wx群机器人发送音乐
    • ¥15 fesafe材料库问题
    • ¥35 beats蓝牙耳机怎么查看日志
    • ¥15 Fluent齿轮搅油
    • ¥15 八爪鱼爬数据为什么自己停了
    • ¥15 交替优化波束形成和ris反射角使保密速率最大化
    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站