-
AuthorSearch Results
-
December 14, 2016 at 2:40 pm #76187
In reply to: Default option on load
AnonymousInactiveHey 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); ?>December 3, 2016 at 1:06 pm #73812In reply to: Set selected item on filter
RossKeymasterHey 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?
October 26, 2016 at 12:19 am #66470In reply to: Modify SQL search query
AnonymousInactiveSo 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!August 16, 2016 at 5:48 pm #54735In reply to: Any way to pre-populate search field?
AnonymousInactiveAwesome! That was a huge help, thanks. In case anyone else is reading this to solve the same problem, here’s how I solved it:
We happen to have a page-search.php template in our child theme. So, I just added this to the very top:
$searchFilterID = 44867; add_filter('sf_input_object_pre', 'insert_search_term_into_advanced_search', 10, 2); function insert_search_term_into_advanced_search ($input_object, $searchFilterID) { $q = isset($_GET['q']) ? htmlspecialchars($_GET['q']) : ''; if($input_object['name']=='_sf_search') $input_object['attributes']['value'] = $q; return $input_object; }
August 16, 2016 at 12:21 pm #54695Topic: Checkboxes default checked attribute
in forum Search & Filter Pro
AnonymousInactiveHi,
I would like to have the checkboxes’ checked attribute automatically set to ‘checked’, when opening the form. I use the following filter function :function catcheck($input_object, $sfid)
{
if($input_object[‘attributes’][‘type’]==’checkbox’)
{
$input_object[‘attributes’][‘checked’]=’checked’;
}return $input_object;
}
add_filter(‘sf_input_object_pre’, ‘catcheck’, 10, 2);It does not work. In my various tests, I manage to act on the
- element that contains the input elements, but not on the input elements themselves.
Thanks for your help.
Olivier
August 1, 2016 at 8:51 am #52893In reply to: Translate ACF Labels with Wp Globus / Qtranslate
AnonymousInactiveThanks Ross!
With this filter it’s possible to access to ‘heading’ label? I’ll need something like this:
function filter_function_name($input_object, $sfid) { $input_object['heading'] = apply_filters( 'the_title', $input_object['heading'] ); return $input_object; } add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
July 21, 2016 at 2:29 pm #51817In reply to: Getting value from url
AnonymousInactiveHello Trevor,
This looks like a script that would work, I guess.
How do I use this?function filter_function_name($input_object, $sfid) { if($input_object['name']=='_my_field_name') { //udpate this field before rendering } return $input_object; } add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
July 8, 2016 at 1:05 pm #50568In reply to: Use meta field for taxonomy value display
AnonymousInactiveHey Davide!
Sorry for the delay – hectic couple of weeks.
Try this:
// Relabel Co-Authors in Search & Filter function filter_function_name($input_object, $sfid) { if ($input_object['name'] == '_sft_author') { global $coauthors_plus; // Update option labels before rendering foreach($input_object['options'] as $key => $option) { // Rename "all items" label - this is always the option with no value if($option->value=="") { $option->label = "All Authors"; } else { $user = $coauthors_plus->get_coauthor_by( 'user_nicename', $option->value ); $input_object['options'][$key]->label = $user->last_name . ', ' . $user->first_name . ' ' . ' (' . $option->count . ')'; } } $sortArray = array(); foreach($input_object['options'] as $option) { foreach($option as $key => $value) { if(!isset($sortArray[$key])) { $sortArray[$key] = array(); } $sortArray[$key][] = $value; } } $orderby = "label"; array_multisort($sortArray[$orderby],SORT_ASC,$input_object['options']); } // Return return $input_object; } add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
June 27, 2016 at 2:13 pm #49503In reply to: Use meta field for taxonomy value display
AnonymousInactiveThanks, Ross! 🙂
Sorry, Davide! I haven’t had a chance to look at this yet. Work got kinda busy. Bleurrrrgh!
As far as putting the last name first bit goes, that’s easy. All you have to do is change display_name to a combination of last_name, first_name. The code would look something like this:
// Relabel Co-Authors in Search & Filter function filter_function_name($input_object, $sfid) { if ($input_object['name'] == '_sft_author') { global $coauthors_plus; // Update option labels before rendering foreach($input_object['options'] as $key => $option) { // Rename "all items" label - this is always the option with no value if($option->value=="") { $option->label = "All Authors"; } else { $user = $coauthors_plus->get_coauthor_by( 'user_nicename', $option->value ); $input_object['options'][$key]->label = $user->last_name . ', ' . $user->first_name . ' ' . ' (' . $option->count . ')'; } } } return $input_object; } add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
As for sorting, I think Ross is right and that the best way to go about it is using one of the standard PHP sorts.
I’ll have a look at doing that in the next couple of days. Would be a useful one to figure out! 🙂
June 19, 2016 at 11:45 pm #48864In reply to: Use meta field for taxonomy value display
AnonymousInactiveDone!
Added back the “All Authors” title and my post count. Final code:
// Trial by fire function filter_function_name($input_object, $sfid) { if ($input_object['name'] == '_sft_author') { global $coauthors_plus; //udpate this field before rendering foreach($input_object['options'] as $key => $option) { if($option->value=="") {//the option with no value is always the "all items" or unselected state $option->label = "All Authors"; } else { $user = $coauthors_plus->get_coauthor_by( 'user_nicename', $option->value ); $input_object['options'][$key]->label = $user->display_name . ' (' . $option->count . ')'; } } } return $input_object; } add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
-
AuthorSearch Results
-
Search Results
-
Hi,
I would like to have the checkboxes’ checked attribute automatically set to ‘checked’, when opening the form. I use the following filter function :function catcheck($input_object, $sfid)
{
if($input_object[‘attributes’][‘type’]==’checkbox’)
{
$input_object[‘attributes’][‘checked’]=’checked’;
}return $input_object;
}
add_filter(‘sf_input_object_pre’, ‘catcheck’, 10, 2);It does not work. In my various tests, I manage to act on the
- element that contains the input elements, but not on the input elements themselves.
Thanks for your help.
Olivier