0

I am using the below function to set a custom post type taxonomy to a certain term based on the current date. This is for an event that lists upcoming, ongoing, and future events at a venue.

I need the taxonomy term to automatically update based on the current date. Right now, this function only works if I go into the post and click update so that the function runs on save_post.

Someone else mentioned I need to use javascript for this, but I'm dumb when it comes to js.

Here is the function:

//Set post_status based on the current date
function set_event_status($post_ID) {
    $start = strtotime(get_field('start_date')); 
    $end = strtotime(get_field('end_date'));
    $now = strtotime('now - 7 hours');
    if ($start && $end) {
        if (($start <= $now) && ($end >= $now)) {
            $status = 'playing_now';
        } elseif ($start >= $now) { 
            $status = 'opening_soon';
        } elseif ($end < $now) {
            $status = 'closed';
        }
    } elseif ($start) {
        if ($start <= $now) {
            $status = 'closed';
        } elseif ($start >= $now) {
            $status = 'opening_soon';
        } elseif ($start == $now) {
            $status = 'playing_now';
        }
    } else {
        $status = 'closed';
    }
    wp_set_post_terms( $post_ID, $status, 'event_status' );
    return $post_ID;
}
add_action( 'save_post', 'set_event_status' );
  • I'm reading up on wp_cron. It seems to be more for plugin developers, but so far what it does seems like what I need. Anyone have advice on going that route? – Eckstein Sep 1 '13 at 5:09
1

You just need to set it up like this:

if ( ! wp_next_scheduled( 'check_event_status' ) ) {
  wp_schedule_event( time(), 'daily', 'check_event_status' );
}

add_action( 'check_event_status', 'set_event_status' );

install the Debug bar plugin and add the cron extension for it if You need some debuging info about the cron job and to be able to manually delete if needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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