Display multi-day events correctly on events calendar

  • Hi

    I’m bullding a simple events calendar using Generate Blocks and Advanced Custom Fields with 2 date fields:

    event_start_date – mandatory
    event_end_date – optional for use with multi-day events

    To control the query loop I’ve used the nifty PHP Snippet and CSS class you provided here:

    https://generate.support/topic/creating-an-upcoming-events-page-with-query-loops-date-handling/

    This works a treat except for multi-day events which drop from the display before the event end date has been reached.

    Is there a way to add a “event is visible until event_end_date is expired” command using the optional event_end_date field?

  • Hi there,

    Try this PHP code:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    
        // Apply filter if loop has class: custom-event-query
        if ( 
            ! is_admin() && 
            ! empty( $attributes['className'] ) && 
            strpos( $attributes['className'], 'custom-event-query' ) !== false 
        ) {
    
            // Current date in WordPress timezone.
            $today = current_time( 'Y-m-d' );
    
            /*
             * Show an event when:
             *
             * 1. It has an end date and the end date is today or later
             *
             * OR
             *
             * 2. It has no end date and the start date is today or later
             */
            $query_args['meta_query'] = array(
                'relation' => 'OR',
    
                // Event has an end date that hasn't passed.
                array(
                    'key'     => 'event_end_date',
                    'value'   => $today,
                    'compare' => '>=',
                    'type'    => 'DATE',
                ),
    
                // Event has no end date, so start date must be today or later.
                array(
                    'relation' => 'AND',
    
                    array(
                        'key'     => 'event_start_date',
                        'value'   => $today,
                        'compare' => '>=',
                        'type'    => 'DATE',
                    ),
    
                    array(
                        'relation' => 'OR',
    
                        // End date doesn't exist.
                        array(
                            'key'     => 'event_end_date',
                            'compare' => 'NOT EXISTS',
                        ),
    
                        // End date exists but is empty.
                        array(
                            'key'     => 'event_end_date',
                            'value'   => '',
                            'compare' => '=',
                        ),
                    ),
                ),
            );
    
            // Order events by start date.
            $query_args = array_merge( $query_args, array(
                'meta_key' => 'event_start_date',
                'orderby'  => 'meta_value',
                'order'    => 'ASC',
            ));
        }
    
        return $query_args;
    
    }, 10, 2 );
  • Works great. You’ve made a nifty PHP Snippet even niftier – and saved me faffing about with a complex calendar plugin for a simple task.

    Thanks.

  • You are welcome   🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.