Forums › Forums › Search & Filter Pro › Using choice fields to filter preset date ranges
- This topic has 1 reply, 2 voices, and was last updated 4 years ago by Trevor.
-
Anonymous(Private) December 22, 2020 at 7:52 pm #270864
Hi,
I am trying to create preset selections of date ranges, but I can’t find the right combination of date/choice fields and Post Meta inputs to make this happen.
Say I have an ACF field called “comic_publish_date”, and I want to filter based on this Post meta.
I would like to have choice options:Latest
Golden Age
Silver Agewhich filter results based on the relevant time period. Eg Golden Age – posts where the publish date is: < 01/01/1957
It seems the date field is a date picker requiring manual entry, so I would need a choice field.
But creating manual choices and entering ” < 19570101″ does not work, it seems to want a specific singular date.Any ideas how I can achieve this?
Thanks,
AndrewTrevor(Private) December 23, 2020 at 8:21 am #270878With a date field, there are only two ways I can think to do this.
1. Have an additional custom field in the posts where one of those 3 labels is the choice.
2. Use code. How this would work is:# Set the form UI field to a number range field, like this (in this case I chose a radio field, but a dropdown would also work, I think):
https://www.screencast.com/t/rpY6G9KLnX
Ignore my field name! Use what you have as a field name.
Then add code like this to your child theme functions.php file (change the field name, form ID number and dates to work for you):
function update_field_options($input_object, $sfid){ // ensure we are only filtering the correct field name in the correct form - in this case the field we want to filter has the name '_sfm_first_date_to', based on the field name 'first_date_to' if( $input_object['name'] == '_sfm_first_date_to' && $sfid == 42 ) { // if we want to filter the options generated, we need to make sure the options variable actually exists before proceeding (its only available to certain field types) if( ! isset( $input_object['options'] ) ) { return $input_object; } //udpate this field before rendering //all the options are stored in $input_object['options'] as an array $new_options = array(); //create a new "default" option $new_option = new StdClass(); $new_option->value = ""; $new_option->label = "All Ages"; array_push($new_options, $new_option); //create a new range option we want to add $new_option = new StdClass(); $new_option->value = "19900101+29991231"; //this is the value that goes in the URL, which affects the query $new_option->label = "Latest"; //the label can be anything array_push($new_options, $new_option);//create a new range option we want to add $new_option = new StdClass(); $new_option->value = "0+19561231"; //this is the value that goes in the URL, which affects the query $new_option->label = "Golden Age"; //the label can be anything array_push($new_options, $new_option); $new_option = new StdClass(); $new_option->value = "19570101+19891231"; //this is the value that goes in the URL, which affects the query $new_option->label = "Silver Age"; //the label can be anything array_push($new_options, $new_option); //now replace the options with our own custom options: $input_object['options'] = $new_options; } return $input_object; } add_filter('sf_input_object_pre', 'update_field_options', 10, 2);
You can see what it looks like on this demo I made (it is not functional as the data does not match!!):
https://sandfdev.cdnwebservices.net/shop/
I hope that makes sense!
It assumes that the date data is correctly stored in ACF/SQL date format(YYYYMMDD).
-
AuthorPosts