AnonymousInactive
Thanks for the link.
I fixed it the following way:
function filter_function_name($input_object, $sfid) {
if($input_object['name']=='xxx') {
$input_object['defaults'] = array("yyy");
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
AnonymousInactive
Hi,
I need to apply a category filter onload rather than showing all categories. I’ve used the below code to set the default to ‘news’ and removed the all option. But it still loads all the posts rather than just the news post on load.
add_filter(‘sf_input_object_pre’, function ($input_object, $sfid)
{
if(($input_object[‘name’]!=’_sft_category’))
{
return $input_object;
}
$input_object[‘defaults’] = array(“news”);
array_shift($input_object[‘options’]);
return $input_object;
} , 10, 2);
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
AnonymousInactive
I tried using CSS, unfortunately Macs/ Safari refuse to honor display:none;
Regarding the code
// add indent to dropdown
function filter_function_name($input_object, $sfid87) //I cant change it to just 87 as its expecting a variable
{
if($input_object[‘name’]==’_my_field_name’) // this should be the class name?
{
//udpate this field before rendering
$input_object = ” “. $input_object;
}
return $input_object;
}
add_filter(‘sf_input_object_pre’, ‘filter_function_name’, 10, 2); // what is 10,2 is that something that I should be changing?
Sorry to be such a bother.
AnonymousInactive
Thank you.
My php isn’t that great, would it would be something like;
function filter_function_name($input_object, $sfid=87)
{
if($input_object[‘name’]==’sf-level-1′)
{
//udpate this field before rendering
$input_object[‘attributes’][‘style’] = ‘margin-left:50px;’;
}
return $input_object;
}
add_filter(‘sf_input_object_pre’, ‘filter_function_name’, 10, 2);
AnonymousInactive
All of a sudden the search forms have stopped sorting. The page is here: https://intrepidexposures.com/photo-tours/
Tweaks to code as per previous threads here:
// Search Filter
function filter_input_object($input_object, $sfid) {
if(($input_object['name'] == '_sfm_workshop_location') || ($input_object['name'] == '_sfm_tour_style') || ($input_object['name'] == '_sfm_tour_leader')) {
$new_options = array(); //the options added to this will replace all existing optoins in the field
foreach($input_object['options'] as $option) {
if ($option->value !== "")
{
//check to see if the option has a parent
$parent_id = wp_get_post_parent_id($option->value);
if(!$parent_id)
{
//then this option does not have a parent, so it must be a parent itself, add it to the new array
$option->label = get_the_title($option->value);
array_push($new_options, $option);
}
}
}
//now we have an array with only parent options in, so simply replace the one in the input object
$input_object['options'] = $new_options;
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
Any ideas?
AnonymousInactive
Hey Trevor,
Thank you.
After numerous other options I tried the below but not sure I’m in the right zone (the custom class is added to the field so it’s partly working).
I just want to pull one of the options from that dropdown as the default on that page (selected option on page load).
Any pointers?
Thank you!
<?php function filter_input_object($input_object, $sfid) {
if ($input_object[‘name’] == ‘_sft_testcat’) {
$input_object[‘defaults’] = array(“Test”);
$input_object[‘attributes’][‘class’] = ‘mynew_class’;
}
return $input_object;
}
add_filter(‘sf_input_object_pre’, ‘filter_input_object’, 10, 2); ?>
Hey Ash
The best way to get the current value is to use our active query class:
https://www.designsandcode.com/documentation/search-filter-pro/accessing-search-data/
Because you want the actual value it might be best to use the array method and pull your value from there.
Alternatively, when using the sf_input_object_pre filter, the defaults should already be set to the current value from the URL, as its this default argument that is set by S&F and will be used to set the selected state.
Doing a var_dump of $input_object['defaults'] should show the currently active value, if you access it at the top of your function before you’ve made any modifications (note: this will always be an array even for single selection type fields)
Hope that makes sense?
AnonymousInactive
I’m trying to set dropdown options programmatically.
But all of options on dropdown come out with “selected” attribute.
How do I set selected attribute properly to single option currently selected?
function sf_input_object_pre_mod($input_object,$sfid){
if($sfid==123){
if($input_object['name']=='_sfm_event_month'){
$mons[] = date('Y-m', strtotime(date('Y-m-01')));
for($i=1;$i<=11;$i++){ $mons[] = date('Y-m', strtotime(date('Y-m-01').' +'.$i.' month')); }
foreach($mons as $k=>$m){ $opt = new StdClass();
$opt->label = $m; $opt->attributes = array('class'=>'sf-level-0'); $opt->values = $m; $opt->count = 0;
$input_object['options'][] = $opt; }
}
}
return $input_object;
}
add_filter('sf_input_object_pre','sf_input_object_pre_mod',10,2);
AnonymousInactive
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!