Hi there
I understand the scenario.
We have a filter, to change the default in the search form, but this won’t affect the results.
This would be the code (untested ofc):
function filter_input_object_categories($input_object, $sfid){
//ensure we are only filtering the correct field name - in this case the field we want to filter has the name <code>_sft_marketplace</code>
if($input_object['name']!='_sft_marketplace'){
return $input_object;
}
//manually set the values shown/selected in the field - must be array even if single value
//this should really only occur under specific conditions, otherwise your field will be permanently set to this value
//if the field is not already in use / selected
if(!isset($_GET['_sft_marketplace'])){
//then set it to "best wordpress app themes"
$input_object['defaults'] = array("best-wordpress-app-themes");
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_input_object_categories', 10, 2);
Thanks
AnonymousInactive
Thanks for the help Trevor! fixed it with the below code, nice one.
function yothop_sf_filter($input_object, $sfid)
{
if($input_object[‘name’]==’_sfm_location’)
{
$options = $input_object[‘options’];
$new_options = [];
foreach ( $options as $option=>$value ) {
$is_numeric = is_numeric($value->value);
if(!$is_numeric){
$new_options[] = $value;
}
}
$input_object[‘options’] = $new_options;
}
return $input_object;
}
add_filter(‘sf_input_object_pre’, ‘yothop_sf_filter’, 10, 2);
AnonymousInactive
Ok, so if I understand well the plugin does allow to render results on page load, but doesn’t allow to filter those results.
What about the following:
Is the sf_input_object_pre filter hook not called on page load? If it is, I could check if the ‘from data’ field is empty and if so I’d add the present date to the $input_object.
Could this work?
Thank you in advance
If you mean how they display in the dropdown select in the form, you would need to do this using our https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object filter.
I do not know if there are any snippets showing how other users have done anything similar, but you might find the posts that this search returns useful:
https://support.searchandfilter.com/forums/search/sf_input_object_pre+sort/
AnonymousInactive
Hello Team,
No matter where I search, I can’t find a way to make the plugin run on page load.
Once I start clicking around, the ajax filtering works fine, but I need it to work on the first page load too.
We are using the code below to remove one of the options and to set the default active item. This all works fine.
// Remove “All Courses” option and set main to default on Our Menu.
add_filter( ‘sf_input_object_pre’, function ( $input_object, $sfid ) {
if ( $sfid == 120 ) {
// var_dump($input_object);
if ( ( $input_object[‘name’] = ‘_sft_course’ ) ) {
if ( empty( $input_object[‘defaults’][0] ) ) {
$input_object[‘defaults’] = [ ‘main’ ];
}
$key = array_search( ‘All Courses’, array_column( $input_object[‘options’], ‘label’ ) );
if ( $key !== false ) {
unset( $input_object[‘options’][ $key ] );
$input_object[‘options’] = array_values( $input_object[‘options’] );
}
}
}
return $input_object;
}, 10, 2 );
Can you please tell me how to instruct the plugin to run in the fresh page load?
I can’t find the answer anywhere in the docs or internet.
Thank you very much,
Kota
AnonymousInactive
Ok I got this working! Maybe not the best way possible but it works. Here is what I have done:
function filter_input_object($input_object, $sfid) {
if(($input_object['name']!='_sfm_colors')||($input_object['type']!='select')) {
return $input_object;
}
if(!isset($input_object['options'])) {
return $input_object;
}
// Move Green To Top
$key = array_search('Green', array_column($input_object['options'], 'value'));
$top = $input_object['options'][$key];
unset($input_object['options'][$key]);
array_unshift($input_object['options'], $top);
// Move All Colors Back to Top
$key1 = array_search('', array_column($input_object['options'], 'value'));
$top1 = $input_object['options'][$key1];
unset($input_object['options'][$key1]);
array_unshift($input_object['options'], $top1);
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
If there is a cleaner way I am interested, but this does work.
AnonymousInactive
No that’s not the entire code i was using, I was trying to show the filter and function I was using to try and access the object value. I have based my attempts on this github code.
I have tried this:
function filter_input_object($input_object, $sfid) {
if(($input_object['name']!='_sfm_colors')||($input_object['type']!='select')) {
return $input_object;
}
if(!isset($input_object['options'])) {
return $input_object;
}
foreach($input_object['options'] as $option) {
$green = $option->value == 'Green';
}
array_splice( $input_object['options'], 1, 0, array($green) );
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
It returns blank
I just tried this:
function filter_input_object($input_object, $sfid) {
if(($input_object['name']!='_sfm_colors')||($input_object['type']!='select')) {
return $input_object;
}
if(!isset($input_object['options'])) {
return $input_object;
}
$green = $input_object->value == 'Green';
array_splice( $input_object['options'], 1, 0, array($green) );
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
It also returns blank
AnonymousInactive
Yes. So for example, I am using:
function filter_input_object($input_object, $sfid) {
if(($input_object['name']!='_sfm_colors')||($input_object['type']!='select')) {
return $input_object;
}
if(!isset($input_object['options'])) {
return $input_object;
}
}
add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
But when I try to access a specific option, I have not been able to do so with success. I am not sure of the array structure which is part of the issue.
AnonymousInactive
Hi Trevor,
This is the code we’ve used btw. Maybe it’s useful 🙂
function filter_function_name($input_object, $sfid)
{
if($input_object['name']=='_sfm_kalender_datum')
{
foreach($input_object['options'] as $option) {
if($option->value != "") {
$date = strtotime( $option->label );
$option->label = date('d m Y', $date);
}
}
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
You might be able to do this using our Input Object filter. This would allow you to remove the child terms, but you would need to write the code for that, using the code snippets linked to, or a search on our forum for other snippets that users may have posted:
https://support.searchandfilter.com/forums/search/sf_input_object_pre/