Forums Forums Search Search Results for 'sf_input_object_pre range'

Viewing 3 results - 11 through 13 (of 13 total)
  • Author
    Search Results
  • #105123

    Ross
    Keymaster

    Hi Javier

    You can do this with the current version..

    Take a look at the docs here:

    https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Filter_Input_Object

    Nearly any value in any input object can be changed if you dig deep enough into the $input_object there are all kinds of things that can be changed.

    Tested locally, and updated to match your field name _sfm_precio, this should do the trick:

    function filter_range_dropdown($input_object, $sfid)
    {
    	
    	if($input_object['name']=='_sfm_precio')
    	{
    		//udpate this field before rendering
    		//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;
    		}
    		
    		//now loop through the options in the field
    		foreach($input_object['options'] as $option)
    		{
    			//update every label, and prefix and suffix with $
    			$option->label = "$ ".$option->label." $";
    		}		
    	}
    
    	//always return the input object, even if we made no changes
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'filter_range_dropdown', 10, 2);

    Hope that helps

    #66470

    Anonymous
    Inactive

    So here’s the complete solution to my situation – adding extra filtering to a S&F SQL query (using a S&F input field to pass the data) after the S&F does the initial SQL query using the rest of the input components in the S&F search form.

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     * Modify the Search&Find SQL query using a S&F input component to pass values through.
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    
    //First filter & modify the input field we're going to use so S&F will not use it for it's own query.
    //I used '_search-filter-settings' but you can use any meta_field in your WP DB.
    //Be sure to grab the field name from the <SELECT> element name in the form, OR add the prefix _sfm_ to the meta_key name.
    add_filter('sf_input_object_pre', 'mbs_alter_snf_field', 10, 2);
    function mbs_alter_snf_field($input_args, $sfid){
    	//+SET S&F FIELD TO USE
    	$mbs_snf_field='_sfm__search-filter-settings';
    
    	if(!isset($input_args['name']) || $input_args['name'] != $mbs_snf_field)
    		return $input_args;
    
    	$input_args['attributes']['name']='mbs'.$mbs_snf_field;
    	return $input_args;
    }
    
    //Secondly, filter & modify the S&F query after S&F did it's filtering based on the other search fields in the form.
    //Make changes to the WHERE clause of that resulting query, based on the input received from $mbs_snf_field input component.
    add_filter( 'posts_where' , 'mbs_posts_where_statement' );
    function mbs_posts_where_statement( $where ) {
    	global $wp_query;
    
    	//+SET FORM ID TO REACT UPON
    	$snf_form=345; 
    
    	//+SET S&F FIELD TO USE
    	$mbs_snf_field='_sfm__search-filter-settings';
    
    	//check if we're in Search context before altering the query
    	if ( $snf_form != $wp_query->query_vars['sfid'] ) return $where;
    
     	//check if this is a Search query or just displaying the search widget
     	if(strpos($where, 'mbs_posts.ID IN')===FALSE) return $where;
    
     	//check if the desired request field has been passed
     	if(!isset($_REQUEST['mbs'.$mbs_snf_field])) return $where;
    
     	//create a array and get values from it if the proper $_REQUEST isset, or default, so we only pass static data to SQL query.
     	$mbs_sql_range=array('10km'=>10,'15km'=>15,'20km'=>20,'25km'=>25,'50km'=>50);
     	$mbs_sql_range=(isset($mbs_sql_range[$_REQUEST['mbs'.$mbs_snf_field]])) ? $mbs_sql_range[$_REQUEST['mbs'.$mbs_snf_field]] : 50;
    
     	//alter the query
     	global $wpdb;
     	$where .= $wpdb->prepare(" AND $wpdb->posts.post_title LIKE '%%%s%%'",$mbs_sql_range) ;
     	
    	//removes the actions hooked on the '__after_loop' (post navigation)
    	remove_all_actions ( '__after_loop'); //* take this out if you don't need it.
     
    	return $where;
    }

    There is only one issue remaining, the input field used to pass the data, does not retain its selection when subsequently displaying the form. And that’s normal and not a very big issue.
    Ross, if you have some insight on this matter, that’ll be great!

    #46666

    In reply to: Dynamic slider range


    Ross
    Keymaster

    Hey Arek

    I’ve just done a quick update to the plugin which should allow you to change the slider min / max settings (not their actual values).

    Install the update I’ve just emailed to you, by following:

    1) disable S&F (do not delete via wp-admin)
    2) via FTP upload and replace the existing S&F in – wp-content/plugins/search-filter-pro/
    3) enable S&F via wp-admin

    After this you can use the following filter to change the min max:

    
    function change_slider_min_max($input_object, $sfid)
    {
    	if(($input_object['name']=='_sfm__price')&&(($input_object['type']=='range-slider')||($input_object['type']=='range-number')))
    	{
    		$input_object['attributes']['data-min'] = 10;
    		$input_object['attributes']['data-max'] = 200;
    		
    	}
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'change_slider_min_max', 10, 2);

    Check here for a full example of how to use the filter:

    https://gist.github.com/rmorse/7b59b45a14b1ca179868

    Thanks

Viewing 3 results - 11 through 13 (of 13 total)