Expire post to draft by date-picker custom field

admin2025-01-08  5

Need to set post to draft when it reaches date from ACF date-picker field. This is code I'm using:

// expire offer posts on date field.
if (!wp_next_scheduled('expire_posts')){
  wp_schedule_event(time(), 'twicedaily', 'expire_posts'); // this can be hourly, twicedaily, or daily
}

add_action('expire_posts', 'expire_posts_function');

function expire_posts_function() {
    $today = date('Ymd');
    $args = array(
        'post_type' => array('event'), // post types you want to check
        'posts_per_page' => -1 
    );
    $posts = get_posts($args);
    foreach($posts as $p){
        $expiredate = get_field('ev_date', $p->ID, false, false); // get the raw date from the db
        if ($expiredate) {
            if($expiredate < $today){
                $postdata = array(
                    'ID' => $p->ID,
                    'post_status' => 'draft'
                );
                wp_update_post($postdata);
            }
        }
    }
}

It is not working. What I'm doing wrong? This is my field settings:

And source: ACF forum

Need to set post to draft when it reaches date from ACF date-picker field. This is code I'm using:

// expire offer posts on date field.
if (!wp_next_scheduled('expire_posts')){
  wp_schedule_event(time(), 'twicedaily', 'expire_posts'); // this can be hourly, twicedaily, or daily
}

add_action('expire_posts', 'expire_posts_function');

function expire_posts_function() {
    $today = date('Ymd');
    $args = array(
        'post_type' => array('event'), // post types you want to check
        'posts_per_page' => -1 
    );
    $posts = get_posts($args);
    foreach($posts as $p){
        $expiredate = get_field('ev_date', $p->ID, false, false); // get the raw date from the db
        if ($expiredate) {
            if($expiredate < $today){
                $postdata = array(
                    'ID' => $p->ID,
                    'post_status' => 'draft'
                );
                wp_update_post($postdata);
            }
        }
    }
}

It is not working. What I'm doing wrong? This is my field settings:

And source: ACF forum

Share Improve this question asked Apr 11, 2021 at 7:52 JurajJuraj 1541 silver badge11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

ACF saves the value as Ymd format in database regardless of display and return format. All you need is a meta query to fetch the correct posts.

function expire_posts_function() {
    
    //Get Expired Posts only
    $expired_posts = get_posts(arrasy(
        'post_type' => 'event',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'meta_query' => array(
            array(
                'key' => 'ev_date',
                'value' => date('Ymd'),
                'compare' => '<',
            ),
        ),
    ));
    
    //Loop through the posts and set status
    if ( $expired_posts ) {
        foreach( $expired_posts as $expired_post ) {
            wp_insert_post(array(
                'ID' => $expired_post->ID,
                'post_status' => 'draft',
            ));
        }
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266503a1126.html

最新回复(0)