-
AuthorSearch Results
-
December 28, 2020 at 12:25 pm #271160
In reply to: Filter by year and separate sort control
TrevorParticipantOK, 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:
December 23, 2020 at 4:54 pm #270963In reply to: Select specific taxomony terms to use in filter
RossKeymasterHi Jessica
I think I see what is happening!
So I just tried your code (albeit changed it for my setup) and the strangest thing was happening, I was getting some checkboxes with labels missing, and some working just fine.
I then realised (and didn’t know) that at least for checkboxes, the array of
options
must be in order.So unsetting specific values leaves “holes” in the array, that for some reason our checkbox code can’t work around for now.
Instead I did this (push the wanted options, into a new array):
function filter_input_object($input_object, $sfid) { if( !in_array( $input_object['name'], array('_sft_pa_color') ) ) { return $input_object; } if( $input_object['name'] == '_sft_pa_color' ) { $new_options = array(); foreach($input_object['options'] as $key => $option) { if ( in_array( $option->value, array( 'gray', 'green') ) ) { $new_options[] = $option; } } $input_object['options'] = $new_options; } return $input_object; } add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
Another way would be to keep your code exactly the way it was, but before returning
$input_object
do:$input_object['options'] = array_values( $input_object['options'] );
This will effectively reset the indexes too.
Let me know how you get on!
Thanks
December 23, 2020 at 8:21 am #270878In reply to: Using choice fields to filter preset date ranges
TrevorParticipantWith 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).
October 6, 2020 at 2:02 pm #262072In reply to: Multipel Tags fields
TrevorParticipantI think it should be this:
//Search & filters hook function update_range_field_mileage($input_object, $sfid) { //make sure we affect the '_sfm_standby_power_esp_kvs_nr' field only if($input_object['name']=='_sfm_standby_power_esp_kvs_nr' && $sfid==1893) { //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 = "kVA"; array_push($new_options, $new_option); //create a new range option we want to add $new_option = new StdClass(); $new_option->value = "10+50"; //this is the value that goes in the URL, which affects the query $new_option->label = "10 – 50"; //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 = "51+200"; //this is the value that goes in the URL, which affects the query $new_option->label = "51 – 200"; //the label can be anything array_push($new_options, $new_option); $new_option = new StdClass(); $new_option->value = "201+400"; //this is the value that goes in the URL, which affects the query $new_option->label = "201 – 400"; //the label can be anything array_push($new_options, $new_option); $new_option = new StdClass(); $new_option->value = "401+700"; //this is the value that goes in the URL, which affects the query $new_option->label = "401 – 700"; //the label can be anything array_push($new_options, $new_option); $new_option = new StdClass(); $new_option->value = "701+1500"; //this is the value that goes in the URL, which affects the query $new_option->label = "701 – 1500"; //the label can be anything array_push($new_options, $new_option); $new_option = new StdClass(); $new_option->value = "1501+2500"; //this is the value that goes in the URL, which affects the query $new_option->label = "1501 – 2500"; //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_range_field_mileage', 10, 2);
October 6, 2020 at 12:59 pm #262056In reply to: Multipel Tags fields
AnonymousInactiveHi Trevor
Thanks.
Can you just check if it is done correctly with form id if($sfid==1893)
Thanks
//Search & filters hook function update_range_field_mileage($input_object, $sfid) { //make sure we affect the '_sfm_standby_power_esp_kvs_nr' field only if($input_object['name']=='_sfm_standby_power_esp_kvs_nr') if($sfid==1893) { //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 = "kVA"; array_push($new_options, $new_option); //create a new range option we want to add $new_option = new StdClass(); $new_option->value = "10+50"; //this is the value that goes in the URL, which affects the query $new_option->label = "10 – 50"; //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 = "51+200"; //this is the value that goes in the URL, which affects the query $new_option->label = "51 – 200"; //the label can be anything array_push($new_options, $new_option); $new_option = new StdClass(); $new_option->value = "201+400"; //this is the value that goes in the URL, which affects the query $new_option->label = "201 – 400"; //the label can be anything array_push($new_options, $new_option); $new_option = new StdClass(); $new_option->value = "401+700"; //this is the value that goes in the URL, which affects the query $new_option->label = "401 – 700"; //the label can be anything array_push($new_options, $new_option); $new_option = new StdClass(); $new_option->value = "701+1500"; //this is the value that goes in the URL, which affects the query $new_option->label = "701 – 1500"; //the label can be anything array_push($new_options, $new_option); $new_option = new StdClass(); $new_option->value = "1501+2500"; //this is the value that goes in the URL, which affects the query $new_option->label = "1501 – 2500"; //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_range_field_mileage', 10, 2);
October 6, 2020 at 11:34 am #262027In reply to: Multipel Tags fields
TrevorParticipantHi
Great to speak with you Sasha. The link for the main function was this:
https://support.searchandfilter.com/forums/topic/range-overlap/#post-231607
Testing the form id would make it look like this:
function update_range_field_mileage($input_object, $sfid) { //make sure we affect the '_sfm_mileage' field only, and only in form ID 12345 if( $sfid == 12345 && $input_object['name']=='_sfm_mileage' ) { //udpate this field before rendering //all the options are stored in <code>$input_object['options']</code> as an array $new_options = array(); //create a new "default" option $new_option = new StdClass(); $new_option->value = ""; $new_option->label = "KM Stand"; array_push($new_options, $new_option); //create a new range option we want to add $new_option = new StdClass(); $new_option->value = "0+49999"; //this is the value that goes in the URL, which affects the query $new_option->label = "0 - 49999"; //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 = "50000+99999"; //this is the value that goes in the URL, which affects the query $new_option->label = "50000 - 99999"; //the label can be anything array_push($new_options, $new_option); $new_option = new StdClass(); $new_option->value = "100000+150000"; //this is the value that goes in the URL, which affects the query $new_option->label = "100000 - 150000"; //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_range_field_mileage', 10, 2);
Do let me know how you get on?
January 20, 2020 at 6:42 pm #231607In reply to: Range overlap
RossKeymasterHi Patrick
It’s not possible within the UI, but you can use a filter to manually update a fields options (and values):
https://searchandfilter.com/documentation/action-filter-reference/#filter-input-objectI’ve done a test, and here is the code you would need to affect your
mileage
field (copy tofunctions.php
in your child theme):function update_range_field_mileage($input_object, $sfid) { //make sure we affect the '_sfm_mileage' field only if($input_object['name']=='_sfm_mileage') { //udpate this field before rendering //all the options are stored in <code>$input_object['options']</code> as an array $new_options = array(); //create a new "default" option $new_option = new StdClass(); $new_option->value = ""; $new_option->label = "KM Stand"; array_push($new_options, $new_option); //create a new range option we want to add $new_option = new StdClass(); $new_option->value = "0+49999"; //this is the value that goes in the URL, which affects the query $new_option->label = "0 - 49999"; //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 = "50000+99999"; //this is the value that goes in the URL, which affects the query $new_option->label = "50000 - 99999"; //the label can be anything array_push($new_options, $new_option); $new_option = new StdClass(); $new_option->value = "100000+150000"; //this is the value that goes in the URL, which affects the query $new_option->label = "100000 - 150000"; //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_range_field_mileage', 10, 2);
Let me know how you get on.
Thanks
August 2, 2018 at 6:44 pm #184484Topic: Allow Blank Range Field
in forum Search & Filter Pro
AnonymousInactiveI have a range field with number inputs. I’ve figured out how to empty the default values out using the ‘sf_input_object_pre’ filter. However, when doing this I get 0 search results upon submitting the form. Basically, I’d like for this field to be optional. How can I achieve this?
November 10, 2017 at 12:17 pm #141135In reply to: Wildcard for empty fields
AnonymousInactiveDo you mean to open the url
https://support.searchandfilter.com/forums/search/sf_input_object_pre/
instead of
https://support.searchandfilter.com/forums/search/sf_input_object_pre+range/ ?I’m afraid i’ll need some more help Trevor, in any case I really appreciate your fast answers…
November 10, 2017 at 9:37 am #141127In reply to: Wildcard for empty fields
TrevorParticipantAre these meta keys being searched with a Range field, because that would cause the problem. At this time you can sort of work around it (by using the Edit Query Argument filter to remove the unwanted range parameters). There will be snippets other users have posted on how they did this if you search for:
sf_input_object_pre range
-
AuthorSearch Results
-
Search Results
-
Topic: Allow Blank Range Field
I have a range field with number inputs. I’ve figured out how to empty the default values out using the ‘sf_input_object_pre’ filter. However, when doing this I get 0 search results upon submitting the form. Basically, I’d like for this field to be optional. How can I achieve this?