I 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
AnonymousInactive
So, here is my code, don’t know how you want to make use for it, but here it is in case it helps someone!
First of all, I just set a couple of values in the admin, 0-100 – it makes no difference since I am unsetting the initial options anyway:
function change_car_engine_size($input_object, $sfid) {
if($input_object['name']=='_sfm_car_engine_size') {
unset($input_object['options']);
$input_object['options']=array();
$arr = ['0','100','200','300','400', '500', '600', '700', '800', '900', '1000','2500','3000','3500','4000'];
foreach ($arr as $value) {
$new_option = new StdClass();
$new_option->value = $value;
$new_option->label = number_format($value, 0, ',', '.');
array_push($input_object['options'], $new_option);
}
}
return $input_object;
}
add_filter('sf_input_object_pre', 'change_car_engine_size', 10, 2);
Hi Thomas
I just did a test, this code, replaces the 4 th option in a list, with what you mention:
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 4th one in the list (including the default), so we can do :
if ( isset ( $input_object['options'][3] ) ) {
$new_option = new StdClass();
$new_option->value = "250+10000000";
$new_option->label = "250 kr and up";
$input_object['options'][3] = $new_option;
}
return $input_object;
}
add_filter('sf_input_object_pre', 'update_field_options', 10, 2);
That should get you going 🙂
Read the comments for a few clues.
Best
Hi Maurino
We have a WP / PHP hook which you can use to modify each individual option in your field – you could then loop through all your options, count the total results, and add that to the first option (All Items) – this is the filter: https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object
I’ve actually gone ahead and managed to create a code snippet for you that does this:
function add_count_to_first_option( $input_object, $sfid ) {
// if the field doesn't have input options, then leave it alone
if ( ! isset( $input_object['options'] ) ) {
return $input_object;
}
$total_count = 0;
// loop through each option and add up the total count
foreach ( $input_object['options'] as $option ) {
if ( isset( $option->count ) ) {
$total_count += absint( $option->count );
}
}
// now check to make sure there was a first option (with value of "")
// and update its count
if ( isset( $input_object['options'][0] ) ) {
if ( '' === $input_object['options'][0]->value ) {
$input_object['options'][0]->label .= ' (' . $total_count . ')';
}
}
return $input_object;
}
add_filter('sf_input_object_pre', 'add_count_to_first_option', 10, 2);
Add this to your themes functions.php, and that will add a count to all first options in your fields.
Thanks
AnonymousInactive
Thank you again Trevor, although in my case, the value actually does exist in the post data. Perhaps it would help if I gave a more specific example of what I am trying to do ie.
Option group: Shop, event or both
Select: Country
Unfortunately countries are stored as a numerical ID in my postmeta which is why I want to use add_filter to change the label for each of them. For now, the code below attempts to change the label for just one country ie. 13 = Austria.
Page loads correctly initially and shows all post types for all countries
If I select one post type plus a country, search results work fine.
If I select both post types, no results are shown.
If I try to Reset the form, no results are shown.
I just cannot understand what the add_filter is doing (or attempting to do) in order to cause such results.
function filter_input_object2($input_object, $sfid)
{
if(($input_object['name']!='_sfm_country_id')||($input_object['type']!='select'))
{
return $input_object;
}
if(!isset($input_object['options']))
{
return $input_object;
}
foreach($input_object['options'] as $option)
{
if($option->value=="13")
{
$option->label = "Austria";
}
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_input_object2', 10, 2);
Note: yes I am aware there are ways of entering the postmeta labels manually and it seems that may be my only option but was reluctant to resort to this as it wouldn’t automatically handle new countries as/when they are added.
AnonymousInactive
Hi there
I have search set up which allows the user to search one or both custom post types (ie. shops and events). They can also choose to search by shop category or event category.
I’m using the code below to add an additional option to the event category select box which works fine IF they select Event as the custom post type to search, however, if they don’t specifically select a custom post type to search (ie. are searching on BOTH types) then no results are returned.
Note: I’m using shortcodes to create the page ie.
[searchandfilter id=”13752″]
[searchandfilter id=”13752″ show=”results”]
The page loads fine initially, showing all 600 records, but as soon as I run a search without selecting a post type, no results are found… and even clicking “Reset” the query has no effect.
Any suggestions would be very gratefully received.
function edit_event_categories($input_object, $sfid) {
if(($input_object['name']!='_sft_ecategory')||($input_object['type']!='select'))
{
return $input_object;
}
if(!isset($input_object['options']))
{
return $input_object;
}
//create a new option we want to add
$new_last_option = new StdClass();
$new_last_option->value = "99";
$new_last_option->label = "Weekend";
//add a brand new option to the bottom
array_push($input_object['options'], $new_last_option);
return $input_object;
}
add_filter('sf_input_object_pre', 'edit_event_categories', 10, 2);
AnonymousInactive
Hello,
I’m using a combobox to display / filter regions on a real estate website. How can I add a new region to the combobox via my functions.php file? Basically, I want to combine a few towns into a single option.
I’ve used the code below to add options to a normal dropdown in the search but couldn’t get something similar to work for the combobox:
function edit_rent_range($input_object, $sfid) {
if(($input_object['name']!='_sfm_rent_max')||($input_object['type']!='select'))
{
return $input_object;
}
if(!isset($input_object['options']))
{
return $input_object;
}
//create a new option we want to add
$new_last_option = new StdClass();
$new_last_option->value = "3000000";
$new_last_option->label = "$35,000+";
//add a brand new option to the bottom
array_push($input_object['options'], $new_last_option);
return $input_object;
}
add_filter('sf_input_object_pre', 'edit_rent_range', 10, 2);
Any help will be appreciated.
AnonymousInactive
We got it working with this code:
function my_plugin_search_filter_change_label($input_object, $sfid) {
if ($sfid == 58513 && $input_object['name'] == '_sf_post_type') {
foreach ($input_object['options'] as $key => $option) {
if ($option->label == 'Posts') {
$input_object['options'][$key]->label = 'Articles';
}
}
}
return $input_object;
}
add_filter('sf_input_object_pre', 'my_plugin_search_filter_change_label', 10, 2);
Hi 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-object
I’ve done a test, and here is the code you would need to affect your mileage field (copy to functions.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
Hi Yanin
This code should do the trick:
function sf_format_date_labels($input_object, $sfid){
//change <code>_sfm_date</code> to the name of your field
if($input_object['name']=='_sfm_date')
{
//udpate this field before rendering
if(!isset($input_object['options']))
{
return $input_object;
}
//now we know there are options we can loop through each one, and change what we need
foreach($input_object['options'] as $option)
{
if($option->value=="")
{//the option with no value is always the "all items" or unselected state
//$option->label = "This could be a default label";
}
else
{
$date = $option->value;
//convert date to YYYY-MM-DD so strtototime doesn't mix up months / days
$input_date = substr($date, 0, 4) . '-' . substr($date, 4, 2). '-' . substr($date, 6, 2);
//now format the date how we want
$date_format = "M j, Y";
$option->label = date($date_format, strtotime($input_date));
}
}
}
return $input_object;
}
add_filter('sf_input_object_pre', 'sf_format_date_labels', 10, 2);
Things to note:
1) change _sfm_date to the name of your field – you can see the name of your field by checking it in the URL, after performing a search with a date value selected
2) To change the display format, modify the line:
$date_format = "M j, Y";
You can format the date using the normal PHP date parameters: https://www.php.net/manual/en/function.date.php
Let me know how you get on.
Thanks