Forums › Forums › Search & Filter Pro › Filter by year and separate sort control
- This topic has 8 replies, 1 voice, and was last updated 3 years, 10 months ago by Trevor.
-
Anonymous(Private) December 24, 2020 at 12:10 am #271022
split by @trevorsf from https://support.searchandfilter.com/forums/topic/filter-by-year-and-separate-sort-control/
Would this sometime in the New Year be something like January? 🙂 We really need the year only drop-down option.
Do you have a workaround until V3 comes out for the year only? I know above you said “if you had a custom field’. DO you have more instructions for that? How do we set that up?
Trevor(Private) December 24, 2020 at 12:43 pm #271054At this time, with the other commitments we have, the earliest I expect V3 would be late February, but that may move back a few weeks.
Is the date field that you want to use only going to be used once in the form, and are the dates stored in the database in the SQL/ACF format (YYYYMMDD)? If the answer to both is yes, a manually coded solution is possible, which I could show you.
Otherwise, the coding gets more complex, needing a second custom field for the year, where the value is auto calculated each time you save a post, as per this post:
https://support.searchandfilter.com/forums/topic/datefilter/page/3/#post-238869
Trevor(Private) December 28, 2020 at 12:25 pm #271160OK, and for anybody who has the same conditions that the previous user has, here is a demo form:
https://sandfdev.cdnwebservices.net/shop/
There are 5 ‘posts’, each has one ‘date’, each in a different year (2016-2020 inclusive).
The ‘Year’ field is based on a custom field named
first_date_from
, and this is an ACF date field with a single date saved in the format YYYYMMDD.The code below is pasted in to the child theme functions.php file:
function update_date_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_from', based on the field name 'first_date_from' if( $input_object['name'] == '_sfm_first_date_from' && $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 Years"; array_push($new_options, $new_option); //create a new range option we want to add $new_option = new StdClass(); $new_option->value = "20160101+20161231"; //this is the value that goes in the URL, which affects the query $new_option->label = "2016"; //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 = "20170101+20171231"; //this is the value that goes in the URL, which affects the query $new_option->label = "2017"; //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 = "20180101+20181231"; //this is the value that goes in the URL, which affects the query $new_option->label = "2018"; //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 = "20190101+20191231"; //this is the value that goes in the URL, which affects the query $new_option->label = "2019"; //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 = "20200101+20201231"; //this is the value that goes in the URL, which affects the query $new_option->label = "2020"; //the label can be anything array_push($new_options, $new_option);//create a new range option we want to add //now replace the options with our own custom options: $input_object['options'] = $new_options; } return $input_object; } add_filter('sf_input_object_pre', 'update_date_field_options', 10, 2);
I hope you can see how it works by actually sending a date range of 1 Jan to 31 Dec of each year to the form. So, if you want more years, add them as you need.
This is how the field is set:
Anonymous(Private) December 31, 2020 at 12:24 pm #271423The ACF date picker field name we set up is
first_date_from
. Above you wrote “custom field namedfirst_date_from
, and this is an ACF date field” Should it be set up differently?The site is behind google authentication during development. I can’t share a link at this time.
Trevor(Private) December 31, 2020 at 12:45 pm #271429Ah. You could have named it anything you liked. My code was simple based on what was on my site.
When you are at a point that you can share access (privately of course), then I would need to take a look. I can’t really help much more at this point.
-
AuthorPosts