0

I have a working script to "expire" a post based on the data of a custom field. The post "expires" (status changes to a custom status archived) on the same day as the event_end_date date.

Instead, I would like the post status to be changed the day AFTER the event_end_date date. I tried to modify my script but it's not working... I don't know PHP, I'm just trying to decipher it from existing code and web searches.

My initial script which works to change the post status on the same day as event_end_date:

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

add_action('hol_expire_events', 'hol_expire_events_function');

function hol_expire_events_function() {
    $today = date('Y-m-d');
    $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_post_meta($p->ID, 'event_end_date', true ); // get the date from the db
        if ($expiredate) {
            if($expiredate < $today){
                $postdata = array(
                    'ID' => $p->ID,
                    'post_status' => 'archived'
                );
                wp_update_post($postdata);
            }
        }
    }
}

And below is my updated script, with an attempt to change the expiring date to the DAY AFTER the date in event_end_date:

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

add_action('hol_expire_events', 'hol_expire_events_function');

function hol_expire_events_function() {
    $nextday = date('Y-m-d', strtotime("+1 day"));
    $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_post_meta($p->ID, 'event_end_date', true ); // get the date from the db
        if ($expiredate) {
            if($expiredate < $nextday){
                $postdata = array(
                    'ID' => $p->ID,
                    'post_status' => 'archived'
                );
                wp_update_post($postdata);
            }
        }
    }
}

It is unfortunately not working, and I'm not sure why. When I run the cron for that function, it still 'expires' the post on the same day as event_end_date instead of on event_end_date + 1 day.

Would anyone be able to help? Thank you in advance.

1 Answer 1

0

Doesn't your script state to expire the post if it's expire date is before tomorrow? Wouldn't that make it expire a day sooner? If it's set to expire the 12th, and it's the 11th, 11+1 = 12, so expire it. Wouldn't you rather either subtract 1 day instead of add 1 day, or add the day to the expiration date instead.

1
  • You're making a lot a sense, yes. However, I realized that since i'm using the < comparator, I don't need to add or subtract any day to the $nextday variable, it can just be "today", since it means posts will only be archived when their $expiredate is 1+ day before today. I think I resolved my issue, but your comment was helpful in clarifying further how it works. Thank you!
    – nomade0
    Commented Dec 24, 2022 at 23:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.