Forums › Forums › Search & Filter Pro › Filter events by upcoming
- This topic has 11 replies, 3 voices, and was last updated 6 years, 3 months ago by Anonymous.
-
Anonymous(Private) July 23, 2018 at 4:44 am #183126
Hi,
I have an event site that I’ve built with ACF and Search and Filter Pro. I’m trying to add a section to my search form that allows users to select a date, specified by a custom field ‘start_date’ created using ACF, which will then output all the events that occur on that date or afterwards. By using the Post Meta – Date field I am able to output all events on that exact date, am I able to modify it to output all the upcoming events as well as the ones on that date?
Thank you!
Trevor(Private) July 23, 2018 at 10:10 am #183150At the moment, our plugin’s date comparison (operator) options do not include >= a give date, but you should be able to modify the operator in the query before the search is actually made using our Edit Query Arguments filter, as long as you always want it to work this way. Here is the documentation:
https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments
Anonymous(Private) July 23, 2018 at 2:51 pm #183172Ok so just to clarify, the search form will display all the events (past, present, and future) when none of the filters have been used. Users are able to enter keywords, change the category etc, and all events that are applicable will still be shown. Only when the date filter is modified, due to the edit query args filter, all the past events will disappear. Is that correct?
Anonymous(Private) July 24, 2018 at 7:24 am #183232Ok, awesome! I took a stab at it today and I’ve written this which pretty much works backwards. It removes all the past events on page load and maintains that when using the keywords and category filter. Then when I enter in a date it reverts to showing just the events that happen on that date, nothing upcoming. Would you be able to help me alter it to achieve what I need? I’m also not sure how to pull the users input from the form, or how to check a specific field if it has been utilized. I apologize if I’ve gone completely down the wrong track!
function upcoming_events( $query_args, $sfid ) { if ( $sfid == 72 ) { $dateField = ''; //get users input, if it exists if ( $dateField != '' ) { $query_args['meta_query'] = array( array( 'key' => 'start_date', 'value' => $dateField, 'compare' => '>=', 'type' => 'NUMERIC' ) ); } } return $query_args; } add_filter( 'sf_edit_query_args', 'upcoming_events', 20, 2 );
Ross Moderator(Private) July 25, 2018 at 7:41 pm #183555Hi Jessica
Can you break down exactly what you want to do.
From reading above, my impression is:
1) You want to hide all upcoming or past posts, if a user has not selected anything in the date filter.
2) But, you want them to be able to select any date, effectively bypassing the above mentioned restrictions to the search?
I think (hope) this might be easier than we’re going about it.
Why not, in your filter above, check to see if if the user has interacted with the date field, if not apply your logic?
This would be as simple as checking
if(!isset($_GET['your_field_name'])){ //do stuff } //always return the query_args even if not modifyied return $query_args;
Thanks
Anonymous(Private) July 26, 2018 at 6:52 am #183569Thank you both so much for your help, I really appreciate it!
I would like for all of the events to show as normal, and have all the keyword, category etc filters work as normal, I just want to change the behaviour of the date filter.
Once a date is selected I would like the results to only include events that occur on that date (which it does already), but to also show events that occur after that date.
I’ve updated my previous query to this which I think should work, except that it seems the date filter’s original programming is overriding my meta query.
function upcoming_events( $query_args, $sfid ) { if ( $sfid == 72 ) { if(isset($_GET['_sfm_start_date'])){ $dateField = $_GET['_sfm_start_date']; $query_args['meta_query'] = array( array( 'key' => 'start_date', 'value' => $dateField, 'compare' => '>=', 'type' => 'NUMERIC' ) ); } } return $query_args; } add_filter( 'sf_edit_query_args', 'upcoming_events', 20, 2 );
By using the print_r($query_args); I am able to see that the query does run when a date is entered, except it seems to be ignored as it doesn’t have any effect on the results. The results still only show events that occur on the exact date which is entered.
How do I go about overriding the date filters current programming?
-
AuthorPosts