简体   繁体   中英

Don't display WordPress posts if time has passed using WP_Query

I have a custom post type of schedule which I can add 'events' with a custom field of Timeslot. This custom field is a repeater using ACF Pro. It contains a date, start time and end time. Date picker and time pickers respectively.

On the homepage I have an image slider that pulls in the artwork and event title and only display 'todays' events, in time order. What I want to do now is to not show events that have passed. So if the current time is 14:00pm for example, the all shows that ended before that time will not show. If the show started at 13:00pm and ends at 15:00pm though, that show will still show, and will appear first. I hope that makes sense?!

Here is my WP_Query so far but after a few attempts, I cannot figure out how to modify it to work as required:

            <?php
                $args = array(
                  'post_type'      => 'schedule',
                  'posts_per_page' => '-1',
                  'meta_query'     => array(
                    'relation'     => 'AND',
                      'date_clause' => array(
                        'key'     => 'schedule_time_slot_%_schedule_show_date',
                        'compare' => '=',
                        'value'   => $day,
                      ),
                      'time_clause' => array(
                        'key'     => 'schedule_time_slot_%_schedule_show_start_time',
                        'compare' => 'EXISTS',
                      ),
                    ),
                    'orderby' => array(
                      'date_clause' => 'ASC',
                      'time_clause' => 'ASC',
                    )
                );
        <?php
            $args = array(
              'post_type'      => 'schedule',
              'posts_per_page' => '-1',
              'meta_query'     => array(
                'relation'     => 'AND',
                  'date_clause' => array(
                    'key'     => 'schedule_time_slot_%_schedule_show_date',
                    'compare' => '=',
                    'value'   => $day,
                  ),
                  'time_clause' => array(
                     'key'     => 'schedule_time_slot_%_schedule_show_start_time',
                     'compare' => '>=',
                     'value'   => time(),
                  ),
                ),
                'orderby' => array(
                  'date_clause' => 'ASC',
                  'time_clause' => 'ASC',
                )
            );

Note the change in the time_clause argument. So only show time_clause when it equals or is later than the time now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM