Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Forums Forums Search Search Results for 'sf_input_object_pre'

Viewing 10 results - 21 through 30 (of 150 total)
  • Author
    Search Results
  • #266986

    Trevor
    Moderator

    Hi

    The code goes in the child theme functions.php file.

    There you can check which page you are on before making changes. Tis forum search will provide other snippets, but you need to experiment with the code and see what you wrote does:

    https://support.searchandfilter.com/forums/search/sf_input_object_pre+function/

    #266721

    Ross
    Keymaster

    Hi 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 by count) :

    
    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

    #265318

    Trevor
    Moderator

    The problem is that the data is not stored in the post meta table field as correctly serialized data (in the normal WordPress format), so, our cache building code sees it as one big term, rather than separate terms.

    This post asked a similar question:

    https://support.searchandfilter.com/forums/topic/explode-custom-field-into-several-records-on-the-cache-table/

    The sf_input_object_pre filter could only work if the values had been cached separately (in fact, chances are you would not have needed your code at all).


    Boris Hoekmeijer
    Participant

    Hi Trevor,
    thanks for the link, but I’m not sure what I should do now.
    I understand I have to enter the field name it concerns:

    
    function filter_function_name($input_object, $sfid)
    {
      if($input_object['name']=='_company_name')
      {
        //udpate this field before rendering
      }
      
      return $input_object;
    }
    add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
    

    But beyond that, I don’t know how to trim it down to 10 and how to sort them by number. I went through the code on the github page that is referred to, but that doesn’t bring me further. Can you help me get this right?

    #264919

    Trevor
    Moderator

    As this is a Taxonomy/Category/Tag field, then you will need to code a solution. You can use this filter in your child theme’s functions.php file:

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    To re-order the terms in the field.

    I am not sure if it will help, but if there are many snippets already in the forum, this forum search should find them:

    https://support.searchandfilter.com/forums/search/sf_input_object_pre+function+order/

    #262980

    Trevor
    Moderator

    If it is a Post Meta field, then you can set the source to Manual and drag and drop them. If it is a Taxonomy, then you will need to code a solution. You can use this filter in your child theme’s functions.php file:

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    To re-order the terms in the field.

    I am not sure if it will help, but if there are many snippets already in the forum, this forum search should find them:

    https://support.searchandfilter.com/forums/search/sf_input_object_pre+function+order/


    T Lucas
    Participant

    While 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

    #262072

    In reply to: Multipel Tags fields


    Trevor
    Moderator

    I 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);
    #262056

    In reply to: Multipel Tags fields


    Sasha Maric
    Participant

    Hi 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);
    #262027

    In reply to: Multipel Tags fields


    Trevor
    Moderator

    Hi

    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?

Viewing 10 results - 21 through 30 (of 150 total)