Ajax Load More with nested meta_query
-
My site uses a CPT called ‘event’. Some events are multi-day, some are single day. The query I have written to display them in a list checks to see if a end date has been added before using the start date as a filter to check whether the event should be displayed as Upcoming or Past.
I need to deploy Ajax Load More in the ‘Past Events’ list to avoid it being huge on load. How can i create a query in the shortcode that first checks whether we have a single- or multi-day event on our hands.
Here’s my query:
<?php date_default_timezone_set('Europe/London'); $date_1 = date('Ymd', strtotime("-999 months")); $date_2 = date('Ymd', strtotime("now")); ?> <?php $past_args = array( 'post_type' => 'event', 'meta_query' => array( 'relation' => 'OR', // check to see if end date has been set array( 'key' => 'end_date', 'compare' => 'BETWEEN', 'type' => 'DATE', 'value' => array($date_1, $date_2), ), // if no end date has been set use event/start date array( 'key' => 'start_date', 'compare' => 'BETWEEN', 'type' => 'DATE', 'value' => array($date_1, $date_2), ) ), 'orderby' => 'meta_value_num', 'order' => 'DESC', 'nopaging' => true ); ?>
The result here is:
- Events which have an end_date are only included if their end_date has passed. Other events are included if their start_date has passed.
- Events are ordered by start_date regardless of whether they have an end_date or not.
Is this possible? I’ve managed something similar in the past but only for situations in which all events have only one date:
<?php echo do_shortcode('[ajax_load_more post_type="event" posts_per_page="12" meta_key="event_date" meta_value="'. $today .'" meta_compare="<" meta_type="DATE" orderby="meta_value_num" offset="12" pause="true" scroll="false" button_label="Show More"]'); ?>
It would be great to recreate this whilst checking for an end_date value first.
Thank you.
- The topic ‘Ajax Load More with nested meta_query’ is closed to new replies.