wp cron – Change a Post’s Status based on the date from a custom field? (for Event posts)


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');
function expire_events() {
    $the_query = get_posts( 'post_type=event' );    
    foreach($the_query as $single_post) {
        $event_end_date=get_post_meta($id, '$event_end_date', true );
                $update_post = array(
                'ID'            => $id,
                'post_status'   =>  'draft',
                'post_type' =>  'event' );

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.

nomade0 2 months 2022-12-12T21:24:06-05:00 0 Answers 0 views 0

Leave an answer