I am trying to:
use Query Filter in my Events CPT view to only show events with end dates greater than or equal to today
(this CPT is not a toolset CPT but is being created by a 3rd party plugin, I've attached a screen cap of its settings)
Link to a page where the issue can be seen: hidden link
I expected to see:
My 1 test event with a future date. (all other events are past)
Instead, I got:
I am not seeing any events with my current query settings. All events are being filtered out.
I've tried:
- tried solutions offered in similar support threads
- tried experimenting with different query filter settings
I have also included a screencap from a theme that natively displays future events using this same plugin in case this is helpful. I am essentially trying to recreate this same loop of future events on a different theme using the same plugin + Toolset Views.
Types date fields are stored as UNIX timestamps, so when you create a Query Filter using TODAY(), that also uses a timestamp so that you are comparing like-with-like.
From your screenshots it looks like your 3rd party post type and custom fields is storing dates as a date, so the comparison would fail.
In which case, you cannot set up this View without using custom code for the date filter.
You have a very good example in your screenshots of what the meta_query should look like when working with this post type and custom date field, and you need to reproduce that for your View.
You will need the wpv_filter_query hook, and you will need to update $view_arguments with the meta_query that you can see is used by the theme.
Nigel, thank you for your reply. I am a novice with PHP but I am attempting your suggestion. I am not understanding what you mean by "you will need to update $view_arguments with the meta_query that...is used by the theme". Can you please elaborate on this?
Here is my function so far, though I am not sure if this is correct or even close:
// Query events that have end date today or future
function boc_events_upcoming_loop( $args ) {
// Query arguments
$args = array(
'post_type' => 'ctc_event',
'meta_query' => array(
array( // only get upcoming events (ending today or in future)
'key' => '_ctc_event_end_date', // the latest date that the event goes to (could be same as start date)
'value' => date_i18n( 'Y-m-d' ), // today's date, localized
'compare' => '>=', // all events with start OR end date today or later
'type' => 'DATE'
),
),
'meta_key' => '_ctc_event_start_date_start_time', // want earliest start date/time first
'meta_type' => 'DATETIME', // 0000-00-00 00:00:00
'orderby' => 'meta_value',
'order' => 'ASC' // sort from soonest to latest
);
// Return modified query
return new WP_Query( $args );
}
add_filter( 'wpv-filter_query', 'boc_events_upcoming_loop');
********************
And, if this could be modified to work and then added to my child theme functions.php, I am not sure how to modify my View in TS so that this filter gets applied.
1. Delete the Query Filter you added to your View settings, we'll add one programmatically
2. Add the following code to your site. (No need to edit your theme files, you can add code snippets at Toolset > Settings > Custom Code).
function tssupp_filter_query( $view_args, $view_settings, $view_id ) {
if ( in_array( $view_id, array( 99 ) ) ) { // Edit the View ID
$view_args['meta_query'] = array(
array( // only get upcoming events (ending today or in future)
'key' => '_ctc_event_end_date', // the latest date that the event goes to (could be same as start date)
'value' => date_i18n( 'Y-m-d' ), // today's date, localized
'compare' => '>=', // all events with start OR end date today or later
'type' => 'DATE'
),
);
}
return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_filter_query', 101, 3 );
You need to edit the ID of the View this relates to.
We are not creating a WP_Query here, we are modifying the arguments that Views will use to create a WP_Query.
I haven't included the ordering options in that custom code, because I think it should work with your View settings, it shouldn't be necessary to override them.
Wow I am sure I never would have been able to create that code myself (and didn't know I could add code to TS this way!). Thank you. I followed all your steps carefully and it does work as long as I select 'run always' instead of 'on demand'.
I do get a warning in the snippet about the security check being missing even though it is not missing, but I am not super concerned about that. But did want to mention.
<?php
function tssupp_filter_query( $view_args, $view_settings, $view_id ) {
if ( in_array( $view_id, array( 15858 ) ) ) { // Edit the View ID
$view_args['meta_query'] = array(
array( // only get upcoming events (ending today or in future)
'key' => '_ctc_event_end_date', // the latest date that the event goes to (could be same as start date)
'value' => date_i18n( 'Y-m-d' ), // today's date, localized
'compare' => '>=', // all events with start OR end date today or later
'type' => 'DATE'
),
);
}
Yes, this script needs to run all the time, not just once.
Also, I think the warning about the security script is because that line with toolset_snippet_security_check needs to be at the top, you have it at the bottom.