-
AuthorSearch Results
-
September 10, 2020 at 12:09 pm #259039
In reply to: How can I change categories or tags order?
TrevorParticipantWhat order would you like, or would it be entirely custom?
You may need to use this filter and your own custom code to do this:
https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object
There are many snippets of code in this forum that use this filter. This forum search should show some of these:
https://support.searchandfilter.com/forums/search/function+sf_input_object_pre/
September 8, 2020 at 5:58 pm #258820In reply to: Prices – change ranges to custom
RossKeymasterI don’t have access to your site anymore, but this will replace all the optinos, with the 3 options you asked for:
function update_field_options($input_object, $sfid){ if( $input_object['name'] != '_sfm__price' ) { return $input_object; } // 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; } // this is an array of all the options, we can remove them all, recreate them, etc $new_options = array(); // so create 3 options: $option1 = new StdClass(); $option1->value = "0+100"; $option1->label = "0 - 100"; array_push( $new_options, $option1 ); $option2 = new StdClass(); $option2->value = "100+250"; $option2->label = "100 - 250"; array_push( $new_options, $option2 ); $option3 = new StdClass(); $option3->value = "250+10000000"; $option3->label = "250 kr and up"; array_push( $new_options, $option3 ); $input_object['options'] = $new_options; return $input_object; } add_filter('sf_input_object_pre', 'update_field_options', 10, 2);
Thanks
September 2, 2020 at 2:09 pm #258119In reply to: Number Ranges Min-Max values
AnonymousInactiveSo, here is my code, don’t know how you want to make use for it, but here it is in case it helps someone!
First of all, I just set a couple of values in the admin, 0-100 – it makes no difference since I am unsetting the initial options anyway:
function change_car_engine_size($input_object, $sfid) { if($input_object['name']=='_sfm_car_engine_size') { unset($input_object['options']); $input_object['options']=array(); $arr = ['0','100','200','300','400', '500', '600', '700', '800', '900', '1000','2500','3000','3500','4000']; foreach ($arr as $value) { $new_option = new StdClass(); $new_option->value = $value; $new_option->label = number_format($value, 0, ',', '.'); array_push($input_object['options'], $new_option); } } return $input_object; } add_filter('sf_input_object_pre', 'change_car_engine_size', 10, 2);
August 28, 2020 at 9:32 am #257605In reply to: Prices – change ranges to custom
RossKeymasterHi Thomas
I just did a test, this code, replaces the 4 th option in a list, with what you mention:
function update_field_options($input_object, $sfid){ // ensure we are only filtering the correct field name - in this case the field we want to filter has the name '_sft_post_tag' if( $input_object['name'] != '_sfm_price' ) { return $input_object; } // 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; } // this is an array of all the options, we can remove them all, recreate them, etc // var_dump( $input_object['options'] ); // we know the option you want to replace is the 4th one in the list (including the default), so we can do : if ( isset ( $input_object['options'][3] ) ) { $new_option = new StdClass(); $new_option->value = "250+10000000"; $new_option->label = "250 kr and up"; $input_object['options'][3] = $new_option; } return $input_object; } add_filter('sf_input_object_pre', 'update_field_options', 10, 2);
That should get you going 🙂
Read the comments for a few clues.
Best
August 14, 2020 at 4:14 pm #256122
AnonymousInactiveFound it by investigating the inspector, but I think the name of each field should be more clearly visible.
Here is my working function:
function top_search_additional_attributes($input_object, $sfid) { if($input_object['name']=='_sf_search' && $sfid==161) { $input_object['attributes']['autocomplete'] = 'off'; $input_object['attributes']['autocapitalize'] = 'off'; $input_object['attributes']['autocorrect'] = 'off'; $input_object['attributes']['spellcheck'] = 'false'; echo $sfid; } return $input_object; } add_filter('sf_input_object_pre', 'top_search_additional_attributes', 10, 2);
June 30, 2020 at 9:16 am #250604In reply to: Total Count
RossKeymasterHi Maurino
We have a WP / PHP hook which you can use to modify each individual option in your field – you could then loop through all your options, count the total results, and add that to the first option (All Items) – this is the filter: https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object
I’ve actually gone ahead and managed to create a code snippet for you that does this:
function add_count_to_first_option( $input_object, $sfid ) { // if the field doesn't have input options, then leave it alone if ( ! isset( $input_object['options'] ) ) { return $input_object; } $total_count = 0; // loop through each option and add up the total count foreach ( $input_object['options'] as $option ) { if ( isset( $option->count ) ) { $total_count += absint( $option->count ); } } // now check to make sure there was a first option (with value of "") // and update its count if ( isset( $input_object['options'][0] ) ) { if ( '' === $input_object['options'][0]->value ) { $input_object['options'][0]->label .= ' (' . $total_count . ')'; } } return $input_object; } add_filter('sf_input_object_pre', 'add_count_to_first_option', 10, 2);
Add this to your themes functions.php, and that will add a count to all first options in your fields.
Thanks
June 29, 2020 at 10:38 am #250388In reply to: Pass right post id in ACF hook
AnonymousInactiveHi Ross,
Thanks for checking!
The result in your screenshot is recently added bij ACF as a test, and always working indeed because they hardcoded a field name and a post id in it:var_dump(get_field_object('#!#etrtodiameter','314322'));
When you look at the html attribute “title” from the filter “ISO diameter” below your screenshot, and you refresh several times, you will see that it is sometimes filled and sometimes not. This attribute is being picked up by this hook in functions.php:
‘
function filter_function_name($input_object, $sfid)
{
$acf_name = substr($input_object[‘name’],5);
//echo ‘ACF-name: ‘ . $acf_name . ‘. ‘;
$field = get_field_object($acf_name); // As a test I added 14313 as a product taxonomy id (2nd parameter), this seems to have no effect (still randomly echoing instructions or not)
$instruction = $field[‘instructions’];
//echo ‘instruction: ‘ . $instruction . ‘. ‘;
//echo ‘field_object: ‘ . get_field_object($acf_name) . ‘. ‘;
if(strlen($instruction)>0)
{
$input_object[‘attributes’][‘title’] = $instruction;
}return $input_object;
}
add_filter(‘sf_input_object_pre’, ‘filter_function_name’, 10, 2);‘
So the question is, how can the stability of the first code snippet be integrated in the second code snippet, while keeping it dynamic; custom fields can be related to multiple taxonomy id’s and post id’s and I never know what products are being shown. Or, while typing, I wonder if a solution could be to pick the first product in a filter and pass the id as and argument for the function in the second code snippet. What do you think? And if this is a good idea, do you have an idea how to build this efficiently?Thanks too for your suggestion about the shortcode/front end loading issue. How can I achieve this when using a custom, dynamically used, template?
June 26, 2020 at 9:50 am #250212Topic: Pass right post id in ACF hook
in forum Search & Filter Pro
AnonymousInactiveHi Trevor,
The hook that you helped me with this week, for adding ACF-fields to filters is working randomly; sometimes it returns the wanted info, sometimes it is empty. I contacted ACF about it and this is their (first) answer: “since you are on a taxonomy archive, I believe passing a second parameter containing a post id would be very helpful to ensure that the get_field_object always retrieves data from this post.”.
I think in that case that I have to pass the post id from the filter, or am I wrong? Do you have a suggestion how to achieve this?
The hook:
function filter_function_name($input_object, $sfid) { $acf_name = substr($input_object['name'],5); //echo 'ACF-name: ' . $acf_name . '. '; $instruction = get_field_object($acf_name)['instructions']; //echo 'instruction: ' . $instruction . '. '; //echo 'field_object: ' . get_field_object($acf_name) . '. '; if(strlen($instruction)>0) { $input_object['attributes']['title'] = $instruction; } return $input_object; } add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
Thanks!
June 23, 2020 at 11:31 am #249752In reply to: Add info button
AnonymousInactiveOk nice, this is working:
function filter_function_name($input_object, $sfid) { $acf_name = substr($input_object['name'],5); if(strlen($acf_name)>0) { $input_object['attributes']['title'] = get_field_object($acf_name)['instructions']; } return $input_object; } add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
It only gives different attribute names based on the field type, for example “title” and “class title” but I wil fix that with JS.
Thanks a lot!
May 21, 2020 at 11:17 am #245284In reply to: ajax is not working
RossKeymasterThis is coming as an option in the admin area in S&F version 3.
For now you can do it with a PHP filter (add this to your child theme’s
functions.php
file):function sf_remove_first_option($input_object, $sfid){ // ensure we are only filtering the correct field name - in this case the field we want to filter has the name '_sft_post_tag' if( $input_object['name'] != '_sft_post_tag' ) { return $input_object; } // 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 we know we have options, remove the first option array_shift( $input_object['options'] ); return $input_object; } add_filter('sf_input_object_pre', 'sf_remove_first_option', 10, 2);
Change
_sft_post_tag
for the name of your field.Thanks
-
AuthorSearch Results
-
Search Results
-
Hi Trevor,
The hook that you helped me with this week, for adding ACF-fields to filters is working randomly; sometimes it returns the wanted info, sometimes it is empty. I contacted ACF about it and this is their (first) answer: “since you are on a taxonomy archive, I believe passing a second parameter containing a post id would be very helpful to ensure that the get_field_object always retrieves data from this post.”.
I think in that case that I have to pass the post id from the filter, or am I wrong? Do you have a suggestion how to achieve this?
The hook:
function filter_function_name($input_object, $sfid) { $acf_name = substr($input_object['name'],5); //echo 'ACF-name: ' . $acf_name . '. '; $instruction = get_field_object($acf_name)['instructions']; //echo 'instruction: ' . $instruction . '. '; //echo 'field_object: ' . get_field_object($acf_name) . '. '; if(strlen($instruction)>0) { $input_object['attributes']['title'] = $instruction; } return $input_object; } add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
Thanks!