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
Looks like sf_input_object_pre only applies to text input field. How do I go about applying IDs or Classes to drop-down lists in the filter?
Have you got switched ON (checked) the Maintain Search Form State setting? Try with it off if you have.
For some fields, you can use the sf_input_object_pre filter, shown here (there is a link to an example usage code snippet as well):
https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Filter_Input_Object
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!
AnonymousInactive
$searchandfilter worked in sf_input_object_pre.
Form works as I expected now.
AnonymousInactive
Is this also work in sf_input_object_pre that you gave me?
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1726)->current_query();
AnonymousInactive
What’s proper way to get query parameter in sf_input_object_pre?
This:
sf_input_object_pre
will allow you to modify the html elements, yes. My bad. I am not sure how you would use it in this context though.
AnonymousInactive
I understand.
BTW, Can’t I use these hook to filter dropdown?
sf_edit_query_args or sf_input_object_pre
https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/