-
AuthorSearch Results
-
December 1, 2020 at 5:12 pm #268437
AnonymousInactiveThanks Trevor, that works perfectly — you’re a star!
Here’s the full working version for anyone else who’s interested:
function update_field_options($input_object, $sfid){ // ensure we are only filtering the correct field name - in this case the field we want to filter has the name '_sft_post_tag' if( $input_object['name'] != '_sfm__price' ) { return $input_object; } // 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; } // this is an array of all the options, we can remove them all, recreate them, etc // var_dump( $input_object['options'] ); // we know the option you want to replace is the 7th one in the list (including the default), so we can do : if ( isset ( $input_object['options'][5] ) ) { $new_option = new StdClass(); $new_option->value = "40000+10000000"; $new_option->label = "40000 plus"; $input_object['options'][5] = $new_option; } return $input_object; } add_filter('sf_input_object_pre', 'update_field_options', 10, 2);
And finally I adjusted the UI options to Max value 40000 so no more options are displayed in the popup selector.
December 1, 2020 at 11:22 am #268340
AnonymousInactiveThanks Trevor,
I tried the following but it hasn’t worked sadly:
function update_field_options($input_object, $sfid){ // ensure we are only filtering the correct field name - in this case the field we want to filter has the name '_sft_post_tag' if( $input_object['name'] != '_price' ) { return $input_object; } // 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; } // this is an array of all the options, we can remove them all, recreate them, etc // var_dump( $input_object['options'] ); // we know the option you want to replace is the 4th one in the list (including the default), so we can do : if ( isset ( $input_object['options'][4] ) ) { $new_option = new StdClass(); $new_option->value = "50000+10000000"; $new_option->label = "50000 plus"; $input_object['options'][4] = $new_option; } return $input_object; } add_filter('sf_input_object_pre', 'update_field_options', 10, 2);
Do you have any ideas?
Many thanks
WarrenNovember 17, 2020 at 12:07 pm #266721
RossKeymasterHi Boris
So I had a little play with this and got it half working – it will be for you to tidy up.
Basically, there is a PHP function for sorting arrays by properties –
usort
(in this case we want to sort the array bycount
) :function cmp_reorder_desc($a, $b) { return $a->count < $b->count; } function reorder_options_count( $input_object, $sfid ) { // change _sft_level for your field name if($input_object['name']!=='_sft_level') { return $input_object; } if ( ! isset( $input_object['options'] ) ) { return $input_object; } if ( ! is_array( $input_object['options'] ) ) { return $input_object; } // $input_object['options'] // this is an array of options // An option has 3 properties (and a few more): //$option->label; //$option->value; //$option->count; //this doesn't take into consideration the first option "All items" - you might want to remove it (it is the first option: ) // $input_object['options'][0] and re-add to the array, after the sorting // we can use the PHP function to sort an array by "count": echo usort( $input_object['options'], "cmp_reorder_desc"); return $input_object; } add_filter('sf_input_object_pre', 'reorder_options_count', 10, 2);
I added some notes in for you, remember to change the field name at the beginning to your own.
Thanks
October 9, 2020 at 2:22 am #262498
AnonymousInactiveWhile this thread is a few months old, here is the code for my solution used for a current project. The code below gets the terms for the taxonomy called ‘commodity_codes’ and then updates the Search and Filter field ‘_sft_commodity_codes’ in my search forms by leaving the value intact for the query and simply updating the label to be a concatenation of the term name and the first 21 characters of the description with camel casing.
Thanks to the gist here: https://gist.github.com/rmorse/7b59b45a14b1ca179868
function filter_function_name($input_object, $sfid) { if($input_object['name']=='_sft_commodity_codes') { //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; } //get the terms for the taxonomy $terms = get_terms('commodity_codes'); //now we know there are options we can loop through each one, and change what we need foreach($input_object['options'] as $option) { foreach($terms as $term) { if($term->name==$option->value) {//we want to change the label for the option "black" - we can feed back in the count number to the label for this field type $option->label = $term->name . ' : ' . ucwords(substr($term->description,0,21)); } } } } return $input_object; } add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
Hope this helps others.
— Tom @NPCrowd : https://npcrowd.com
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?
September 25, 2020 at 10:57 am #260793In reply to: Pods Relationship Fields
RossKeymasterHi Sunny
You can replace the ID’s, by post title, by adding this to
functions.php
:function filter_input_object($input_object, $sfid) { if(($input_object['name'] == '_sfm_podsrelationship')) { foreach($input_object['options'] as $option) { $option->label = get_the_title($option->value); } return $input_object; } else { return $input_object; } } add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
Make sure to change the name of the field to match yours:
_sfm_podsrelationship
To find your field name, just check the URL when you do a search:
https://searchandfilter.com/documentation/accessing-search-data/?sfref=dc#how-to-get-the-field-nameLet me know how you get on.
Thanks
September 23, 2020 at 8:41 am #260434In reply to: Default value select
AnonymousInactivei`m using unset to delete first two option values but i need it to be default, how to do this?? thanks
function filter_function_name($input_object, $sfid)
{
if($input_object[‘name’]==’_sft_tax_news’)
{
/*foreach($input_object[‘options’] as $option)
{
if($option->value==”maironio-lietuviu-literaturos-muziejus”)
{
$option->label = “AAA”;
$option->attributes[‘class’] = “sf-option-active”;
$option->attributes[‘selected’] = “selected”;
}
}*/
unset($input_object[‘options’][0]);
unset($input_object[‘options’][1]);
return $input_object;
//udpate this field before rendering
}
else{
echo “NOOO!”;
return $input_object;
}
}
add_filter(‘sf_input_object_pre’, ‘filter_function_name’, 10, 2);filter_function_name(array(‘name’ => ‘_sft_tax_news’),17402);
September 8, 2020 at 5:58 pm #258820In reply to: Prices – change ranges to custom
RossKeymasterI don’t have access to your site anymore, but this will replace all the optinos, with the 3 options you asked for:
function update_field_options($input_object, $sfid){ if( $input_object['name'] != '_sfm__price' ) { return $input_object; } // 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; } // this is an array of all the options, we can remove them all, recreate them, etc $new_options = array(); // so create 3 options: $option1 = new StdClass(); $option1->value = "0+100"; $option1->label = "0 - 100"; array_push( $new_options, $option1 ); $option2 = new StdClass(); $option2->value = "100+250"; $option2->label = "100 - 250"; array_push( $new_options, $option2 ); $option3 = new StdClass(); $option3->value = "250+10000000"; $option3->label = "250 kr and up"; array_push( $new_options, $option3 ); $input_object['options'] = $new_options; return $input_object; } add_filter('sf_input_object_pre', 'update_field_options', 10, 2);
Thanks
-
AuthorSearch Results