1

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.

1 Answer 1

0

There are several little issues with your code. This is what I use for the exact same thing though and it works:

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

add_action('expire_events', 'expire_events_function');

function expire_events_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('event_and_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);
            }
        }
    }
}

Place this code in your functions.php and load the site one time to set your CRON job. You can text to ensure your CRON is set and run it right away as well with wp-cli or a CRON plugin.

I would suggest next time though to prefix your post types so you know they are your own, for cleaner code, and to reduce potential conflicts. (ie event could be nom_event)

EDIT: Try changing this line:

$expiredate = get_field('event_and_date', $p->ID, false, false);

To this:

$expiredate = get_post_meta($p->ID, 'event_end_date', true );

(this line is what your original code should have looked like as well...without the $ in front of event_end_date)

If this isn't working you can contact me on my site (it's pretty easy to figure out)

18
  • thank you, but it doesn't seem to be working either. Just be sure I went into my custom field settings and forced the same Y-m-d format on the date field saved in the db. I manually triggered the cron but I'm not seeing any update to the posts status. I then went and changed daily to hourly in case that's causing a problem, and I'll see in an hour if anything changed. Because of that time delay it's not easy to test if the script works.
    – nomade0
    Commented Dec 14, 2022 at 22:03
  • In wp cli run this command: wp cron event run expire_events. That will run the event immediately.
    – rudtek
    Commented Dec 15, 2022 at 7:06
  • I installed the WP Crontrol plugin instead as I don't have access to CLI easily. I can see the script in the cron events, and I trigged it manually. (see i.imgur.com/1P5BSea.png) However, the page I'm expecting to change status to Draft did no change... I have to assume the problem comes from the script?
    – nomade0
    Commented Dec 15, 2022 at 22:14
  • In case someone else reads this: I want to clarify that there is a typo in the code rudket posted, the custom field is "event_end_date" instead of "event_and_date". I have made the correction in the script running on my site. So that's not why the script isn't working
    – nomade0
    Commented Dec 15, 2022 at 22:27
  • It looks like you've changed the cron event name and the function. Can you post your code so I can review for you?
    – rudtek
    Commented Dec 15, 2022 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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