Problem: I would like to filter a View of Locations using start and end date custom fields in a child Event post, so that the View shows only Locations having Events that overlap with today's date. In other words, show only Events occurring today.
Solution: There's not an easy way to filter a View of post type A by custom fields in post type B, even if a parent/child relationship exists. I can offer some assistance with a View showing Events whose start and end dates intersect with a User-selected date. Then I can explain your options for getting Location information from those matching Events.
- First, create a View of Events. In the Filter editor panel, insert a filter on your start date custom field. If you cannot see the filter editor panel, scroll to the top of the View editor screen in wp-admin and click "Screen Options" in the top right corner. Check the box to display the Filter Editor panel. Configure the filter to show all Events with start dates greater than or equal to TODAY(), comparing values as numbers. The resulting shortcode will look something like this:
[wpv-control-postmeta field="wpcf-start" default_date="TODAY()" url_param="wpv-wpcf-start"]
- In the Loop Output area, display the title, start, and end date of each matching Event for testing purposes.
- Next, add some custom code to your child theme's functions.php file:
add_filter( 'wpv_filter_query', 'default_today_inclusive', 10, 3 ); function default_today_inclusive ( $query, $view_settings, $view_id ) { if( $view_id == 12345 ) { // defaults $selected_start = strtotime('0:00:00'); $selected_end = strtotime('23:59:59'); // if user selected a start date, update the default selected values accordingly, and unset existing start meta query if ( isset($query['meta_query']) ) { foreach($query['meta_query'] as $key => $meta) { if(isset($meta['key']) && $meta['key'] == 'wpcf-start'){ // get timestamps that represents 12:00 am and 11:59:59 pm on the event start date $selected_start = strtotime(date('m/d/Y', $meta['value']) . ' 0:00:00'); $selected_end = strtotime(date('m/d/Y', $meta['value']) . ' 23:59:59'); unset($query['meta_query'][$key]); } } } // find events with start date less than or equal to selected date // and end date greater than or equal to selected date $args = array( 'relation' => 'AND', array( 'key' => 'wpcf-start', 'value' => $selected_end, 'compare' => '<=', 'type' => 'numeric' ), array( 'key' => 'wpcf-end', 'value' => $selected_start, 'compare' => '>=', 'type' => 'numeric' ) ); // add these arguments to your meta query $query['meta_query'] = isset($query['meta_query']) ? $query['meta_query'] : []; $query['meta_query'][] = $args; } return $query; }
- Replace 12345 with the numeric ID of this View. If your field slugs are not "start" and "end", then the keys "wpcf-start" and "wpcf-end" should be updated as well. Add the "wpcf-" prefixes here.
- Show the parent Location information in the View.
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://toolset.com/documentation/user-guides/date-filters/
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 11 replies, has 3 voices.
Last updated by 6 years, 10 months ago.
Assisted by: Christian Cox.