Quick read: I need to update the value of a custom taxonomy based on two custom field values (dates) automatically, meaning the post updates itself with having to click "update" to change the value.
Long details: I have created a new private taxonomy called event_status. The possible values are "opening_soon", "playing_now", and "closed".
This taxonomy is attached to a custom post type named "event". When a post of this type is created, there is a set of custom fields (using advanced custom fields plugin) that is filled out. One is "Start Date" and the other is "End Date". This is the first and last performances of the Event.
When an Event post is created, the taxonomy event_status is set to "opening_soon" (as the event hasn't happened yet).
What I need to do is change the value of the event_status taxonomy based on the comparison of the current date with the start or end date of the event, as set via custom fields.
I can figure out how to do this by hooking into wp_update_post() but the problem is I want the post status to be updated automatically without me having to hit "Update Post" for it to change. It should just changed based on the current date. Hopefully there is a way to do this.
I was previously trying a solution with a simple function to determine the post status, but soon figured out that I couldn't use a function to query with (obviously) so I'm going for the taxonomy solution instead so I can query for posts by that.
Here is the function I'm currently trying to make work to update the taxonomy on save_post, but this isn't quite working yet either. The bottom portion is for events that only have one performance (so the end_date field is empty) and the top deals with events that have a start AND end date.
//Set post_status based on the current date on wp_update_post()
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' );
var_dump(wp_set_post_terms( $post_ID, $status, 'event_status' );); die;
. Save a post. What do you get?