I have a custom post type "events" on my site.
The events have custom fields for START DATE and END DATE.
On the Archive page I have a working filter that allows users to choose only events between the above dates.
By default it shows ALL events which is unnecessary since some of the events have expired.
i.e. we only want to show events with an END DATE greater than NOW and let visitors filter according to their preference.
See ticket:
https://toolset.com/forums/topic/how-to-sort-and-limit-custom-post-type-archive-listing/
I understand that we should be able to do this with the wpv_filter_query in functions.php
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
I can only get this far with the code.
Can you advise what the "//do something here" portion should be?
//Return events which have not expired i.e. with END dates greater than NOW:
add_filter( 'wpv_filter_query', 'hide_expired_events', 99, 3 );
function hide_expired_events( $query_args ) {
//do something here
}
Hello,
Yes, you are right, you can put your custom codes in the "//do something here" portion, for example:
Add below codes into your theme file "functions.php"
add_filter( 'wpv_filter_query', 'hide_expired_events', 99, 3 );
function hide_expired_events( $query_args, $view_settings, $views_id ) {
if ( $views_id == 123 && !isset($_GET['wpv_filter_submit']) ) { // if it is specific view and by default
$query_args['meta_query'][] = array(
'key' => 'wpcf-end-date',
'value' => current_time('timestamp'),
'compare' => '>=',
);
}
return $query_args;
}
Please replace 123 with your view's ID
More help:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Custom Field Parameters
https://codex.wordpress.org/Function_Reference/current_time
Returns the blog's current local time in the specified format
Unfortunately didnt work.
Can I grant you FTP and site access to take a look?
Yes, please provide the credentials in below private message box
Thanks for the details, since the problem URL is post type archive page, the query is defined by wordpress, so you will need to use WordPress built-in action hook pre_get_posts to change the query, I have have edited your theme file functions.php. change the codes to:
//Return events which have not expired i.e. with END dates greater than NOW:
add_filter( 'pre_get_posts', 'hide_expired_events' );
function hide_expired_events( $query ) {
if ( is_post_type_archive('event') && $query->is_main_query() && !isset($_GET['wpv_filter_submit']) ) { // if it is event archive page and by default
$meta_query = $query->get('meta_query');
$meta_query[] = array(
'key' => 'wpcf-end-date',
'value' => current_time('timestamp'),
'compare' => '>=',
);
$query->set('meta_query', $meta_query);
return;
}
}
Please test again, check if it is fixed.
More help:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
It works fine on the first visit.
i.e. by default it only shows events which have not expired
BUT when a visitor uses any of the search filter parameters it also returns expired events.
e.g. a search for event type "Exhibition" returns an event that has already ended.
Ideally, I would like to NEVER show expired events on this page regardless of the query parameters.
Is that possible?
You can modify this line from:
if ( is_post_type_archive('event') && $query->is_main_query() && !isset($_GET['wpv_filter_submit']) ) { // if it is event archive page and by default
To:
if ( is_post_type_archive('event') && $query->is_main_query() ) { // if it is event archive page
And test again.
Nailed it!
Thank you. You can close the ticket.
Hmmm. ... spoke a little too soon?
It works fine in:
hidden link
BUT
hidden link
throws fatal errors. 🙁
Thanks for the feedback, I have modified the codes to below:
//Return events which have not expired i.e. with END dates greater than NOW:
add_filter( 'pre_get_posts', 'hide_expired_events' );
function hide_expired_events( $query ) {
if ( is_post_type_archive('event') && $query->is_main_query() ) { // if it is event archive page
$meta_query = $query->get('meta_query');
if(!is_array($meta_query)){ // by default
$meta_query = array();
}
$meta_query[] = array(
'key' => 'wpcf-end-date',
'value' => current_time('timestamp'),
'compare' => '>=',
);
$query->set('meta_query', $meta_query);
return;
}
}
Please test again, check if it is fixed. thanks
You got it.
All working perfectly now.
Thank you so much.