I've found many posts on this topic and tried to use their solution, but none has worked so far...
I have an Event
custom post type with an event_end_date
custom field. I'm trying to create a PHP snippet to automatically change the status of an Event post from Published to Draft (or whatever) based on the date in event_end_date
. Basically, the day after the end of the event, I want it to be unpublished and changed to draft.
The code I'm currently using is as follows:
wp_schedule_event(time(), 'daily', 'expire_events');
//add_action('init','expire_events');
function expire_events() {
$the_query = get_posts( 'post_type=event' );
foreach($the_query as $single_post) {
$id=$single_post->ID;
$event_end_date=get_post_meta($id, '$event_end_date', true );
if($event_end_date!=''){
$today=date("Y-m-d");
if($event_end_date<$today){
$update_post = array(
'ID' => $id,
'post_status' => 'draft',
'post_type' => 'event' );
wp_update_post($update_post);
}
}
}
}
This code has been copied/pasted from somewhere else and I've adapted it with my custom field name and post type, but it doesn't seem to be working...
Any help would more than welcome, as I don't understand PHP enough to know if what I'm pasting really makes sense :/
Thank you!
PS: The date format in the function seems to match the date format from the custom field, in case you're wondering. For context: I'm using GeneratePress / GenerateBlocks and MetaBox.