AnonymousInactive
Hi 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?
AnonymousInactive
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!
AnonymousInactive
Ok 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!
This 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
It MIGHT be possible, but only with custom code (I don’t know what this would be though), using this filter:
https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object
In code similar to this (in the child theme functions.php file):
function filter_input_destination($input_object, $sfid)
{
if(($input_object['name']!='_sft_destination')||($input_object['type']!='select'))
{
return $input_object;
}
if(!isset($input_object['options']))
{
return $input_object;
}
// sort the array here, using PHP array_multisort() function.
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_input_destination', 10, 2);
And I would guess make sure it is the correct form ID.
AnonymousInactive
Hello, When implementing a programmatic filter via sf_input_object_pre and $input_object, The auto-count does not seem to update on the other non-modified form fields on the form. The autocount numbers appear to be calculated before the filters on the $input_object have been applied via the sf_input_object_pre filter.
Any ideas how to get the auto count to count afterwards? or to have it refresh the auto count before display?
Thanks!
AnonymousInactive
Thank you again Trevor, although in my case, the value actually does exist in the post data. Perhaps it would help if I gave a more specific example of what I am trying to do ie.
Option group: Shop, event or both
Select: Country
Unfortunately countries are stored as a numerical ID in my postmeta which is why I want to use add_filter to change the label for each of them. For now, the code below attempts to change the label for just one country ie. 13 = Austria.
Page loads correctly initially and shows all post types for all countries
If I select one post type plus a country, search results work fine.
If I select both post types, no results are shown.
If I try to Reset the form, no results are shown.
I just cannot understand what the add_filter is doing (or attempting to do) in order to cause such results.
function filter_input_object2($input_object, $sfid)
{
if(($input_object['name']!='_sfm_country_id')||($input_object['type']!='select'))
{
return $input_object;
}
if(!isset($input_object['options']))
{
return $input_object;
}
foreach($input_object['options'] as $option)
{
if($option->value=="13")
{
$option->label = "Austria";
}
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_input_object2', 10, 2);
Note: yes I am aware there are ways of entering the postmeta labels manually and it seems that may be my only option but was reluctant to resort to this as it wouldn’t automatically handle new countries as/when they are added.
AnonymousInactive
Hi there
I have search set up which allows the user to search one or both custom post types (ie. shops and events). They can also choose to search by shop category or event category.
I’m using the code below to add an additional option to the event category select box which works fine IF they select Event as the custom post type to search, however, if they don’t specifically select a custom post type to search (ie. are searching on BOTH types) then no results are returned.
Note: I’m using shortcodes to create the page ie.
[searchandfilter id=”13752″]
[searchandfilter id=”13752″ show=”results”]
The page loads fine initially, showing all 600 records, but as soon as I run a search without selecting a post type, no results are found… and even clicking “Reset” the query has no effect.
Any suggestions would be very gratefully received.
function edit_event_categories($input_object, $sfid) {
if(($input_object['name']!='_sft_ecategory')||($input_object['type']!='select'))
{
return $input_object;
}
if(!isset($input_object['options']))
{
return $input_object;
}
//create a new option we want to add
$new_last_option = new StdClass();
$new_last_option->value = "99";
$new_last_option->label = "Weekend";
//add a brand new option to the bottom
array_push($input_object['options'], $new_last_option);
return $input_object;
}
add_filter('sf_input_object_pre', 'edit_event_categories', 10, 2);
AnonymousInactive
Hello,
I’m using a combobox to display / filter regions on a real estate website. How can I add a new region to the combobox via my functions.php file? Basically, I want to combine a few towns into a single option.
I’ve used the code below to add options to a normal dropdown in the search but couldn’t get something similar to work for the combobox:
function edit_rent_range($input_object, $sfid) {
if(($input_object['name']!='_sfm_rent_max')||($input_object['type']!='select'))
{
return $input_object;
}
if(!isset($input_object['options']))
{
return $input_object;
}
//create a new option we want to add
$new_last_option = new StdClass();
$new_last_option->value = "3000000";
$new_last_option->label = "$35,000+";
//add a brand new option to the bottom
array_push($input_object['options'], $new_last_option);
return $input_object;
}
add_filter('sf_input_object_pre', 'edit_rent_range', 10, 2);
Any help will be appreciated.
AnonymousInactive
We got it working with this code:
function my_plugin_search_filter_change_label($input_object, $sfid) {
if ($sfid == 58513 && $input_object['name'] == '_sf_post_type') {
foreach ($input_object['options'] as $key => $option) {
if ($option->label == 'Posts') {
$input_object['options'][$key]->label = 'Articles';
}
}
}
return $input_object;
}
add_filter('sf_input_object_pre', 'my_plugin_search_filter_change_label', 10, 2);