Using WP meta_query to show posts by a month between start and finish dates
I have a custom post type called ‘events’, I have a page which displays events by each month. Each event has a start_date and finish_date custom field (using advanced custom fields plugin) which stores the date as yymmdd. If an event spans over two different months it is only appearing in the month of the start date. I’m stuck on how to show it in the month of the finish date as well. For example, if I have an event with a start date of 19/03/2014 and a finish date of 19/06/2013 it should appear in all of the months of March, April, May and June. Currently my code will obviosuly only show in the months where the both the event start and finish date are in the same month (which is not what I need).
I am using the below:
// Get chosen month to display from URL $events_month = sanitize_text_field($_GET["month"]); $events_year = sanitize_text_field($_GET["year"]); // Convert chosen month to display to a timestamp $ts = strtotime("$events_month $events_year"); // Create chosen month start end end dates to use for query $month_start_date = date('Ym01', $ts); $month_end_date = date('Ymt', $ts); $args = array( 'post_type' => 'events', 'posts_per_page' => 50, 'order' => 'ASC', 'orderby' => 'meta_value_num', 'meta_key' => 'start_date', 'meta_query' => array( array( 'key' => 'start_date', 'value' => array($month_start_date, $month_end_date), 'type' => 'numeric', 'compare' => 'BETWEEN' ) ) ); $events = new WP_Query($args);
Any ideas on how I can show events in all months they span over?
- The topic ‘Using WP meta_query to show posts by a month between start and finish dates’ is closed to new replies.