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.