0

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' );
6
  • What doesn't work exactly?
    – s_ha_dum
    Commented Aug 31, 2013 at 22:13
  • Well, the above function isn't updating the terms at all for some reason (problem 1). But besides that, I'm trying to figure out how to get that function to run without having to click "Update" on the post. I want it to read the current date and change the taxonomy based on that automatically.
    – Eckstein
    Commented Aug 31, 2013 at 22:18
  • Do var_dump(wp_set_post_terms( $post_ID, $status, 'event_status' );); die;. Save a post. What do you get?
    – s_ha_dum
    Commented Aug 31, 2013 at 22:29
  • I'm getting array(0) { } as the result.... that doesn't seem good.
    – Eckstein
    Commented Aug 31, 2013 at 22:44
  • Is your taxonomy hierarchical?
    – s_ha_dum
    Commented Aug 31, 2013 at 22:50

1 Answer 1

1

You are passing strings as the second parameter $terms and you have a hierarchical taxonomy. Per the Codex:

For hierarchical terms (such as categories), you must always pass the id rather than the term name to avoid confusion where there may be another child with the same name.

If you follow the source, you will notice that your strings get wiped out.

You need to be using term IDs or alter your taxonomy so that it isn't hierarchical.

You can use get_term_by('name','tax-slug','tax-name') to get the IDs.

As far as getting that update automatically, you will have to do that with Javascript.

1
  • Thank you! Of course it was something simple like that. I will explore js options for the updating.
    – Eckstein
    Commented Aug 31, 2013 at 23:11

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.