Support

Account

Forum Replies Created

  • I know this post is over a year old now, but I recently had a similar problem to kylerumble and wanted to provide a solution on the off-chance that someone else can use it.

    The problem: A company takes part in several events, some of which span multiple days, and wants to highlight them across two sections on their website: Upcoming and Past. The Past section shows all events from the previous year up until (the relative) yesterday with the exception of multi-day events. If an event spans multiple days, it continues to be shown in the Upcoming section until the current date is after the last day of the event.

    Like kylerumble, I didn’t want the editors to have to fill in the date twice if the event only lasts a single day.

    The solution:

    I made three ACF fields – “Multi-day Event” (True / False field), “Start Date,” and “End Date” (both Date Picker fields) – and set a conditional around the Start Date field so it only displays if the “Multi-day Event” field is checked.

    Using Jonathan’s last function as a starting point, I used the save_post action to update the field when necessary:

    // run AFTER ACF saves the $_POST['fields'] data to database
    add_action('acf/save_post', 'single_day_event', 20);
    
    function single_day_event( $post_id ) {
        // Ensure we have ACF fields on the post.
        if( empty($_POST['acf']) ) {
            return;
        }
        
        // Set up our variables with the respective ACF field keys
        $multi_day_event = $_POST['acf']['field_123'];
        $start_date_field = $_POST['acf']['field_456'];
        $end_date_field = $_POST['acf']['field_789'];
    
        /* Update the Start Date field value with the End Date field value if any of the following are true:
        1. The Start Date field is empty
        2a. Multi-day Event is empty and the Start Date doesn't equal the End Date
        2b. Multi-day Event's value is 0 and the Start Date doesn't equal the End Date (This is in case an event is ever set to Multi-day and then later changed to a single day)
        */
        if( ( $start_date_field == '' ) || ( ( $multi_day_event == '' || $multi_day_event == 0 ) && ( $start_date_field != $end_date_field ) ) ){
    		update_field('field_456', $end_date_field, $post_id);
    	}
    	
    }

    Cheers!

Viewing 1 post (of 1 total)