Support

Account

Home Forums ACF PRO Upcoming events (and events that haven't passed it's end date)

Solved

Upcoming events (and events that haven't passed it's end date)

  • Hi guys,

    This might be an easy for you gurus but it might also be very useful to heaps of other people who probably will crack their heads trying to figure this out.

    I’m trying to get my upcoming events list to also display events which have end dates that have not passed today, and sort eveything in the list by start date.

    Using datepicker, my start date is event_start and end date is event_end

    How do I modify what I’ve got to get this? At the moment I can display all upcoming events, but I can’t display an event which has already started, but hasn’t ended yet.

    Here’s my code :

            <?php
    
                $now = date('Y-m-d H:i:s');
    
                $args = array(
                    'post_type'         => 'events',
                    'posts_per_page'    => -1,
                    'meta_query'        => array(
                        'relation'      => 'AND',
                        'date_clause'   => array(
                            'key'       => 'event_start',
                            'compare'   => '>=',
                            'value'     => $now,
                            'type'      => 'DATETIME'
                        )
                    ),
                    'orderby'   => 'meta_value_num'
                );
    
                $the_query = new WP_Query($args);
    
            ?>
  • Hi @sanny,

    here my suggestion:

    <?php
    
                $now = date('Y-m-d H:i:s');
    
                $args = array(
                    'post_type'         => 'events',
                    'posts_per_page'    => -1,
                    'meta_query'        => array(
                        'relation'      => 'OR',
                        'date_upcoming_clause'   => array(
                            'key'       => 'event_start',
                            'compare'   => '>=',
                            'value'     => $now,
                            'type'      => 'DATETIME'
                        ),
                        array(
                            'relation'      => 'AND',
                            'date_started_clause'   => array(
                                'key'       => 'event_start',
                                'compare'   => '<=',
                                'value'     => $now,
                                'type'      => 'DATETIME'
                            ),
                            'date_end_clause'   => array(
                                'key'       => 'event_end',
                                'compare'   => '>=',
                                'value'     => $now,
                                'type'      => 'DATETIME'
                            ),
                        ),
                    ),
                    'orderby' => array(
                        'date_started_clause' => 'ASC',
                        'date_end_clause' => 'ASC',
                        'date_upcoming_clause' => 'ASC',
                    ),
                );
    
                $the_query = new WP_Query($args);
    
            ?>
  • Thanks @khammami You’re a LEGEND!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Upcoming events (and events that haven't passed it's end date)’ is closed to new replies.