dpli36193 2016-06-27 09:27
浏览 57
已采纳

如果使用WP_Query已经过了时间,请不要显示WordPress帖子

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',
                    )
                );
  • 写回答

1条回答 默认 最新

  • doupeng6890 2016-06-27 10:17
    关注
            <?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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?