Forums › Forums › Search & Filter Pro › Select specific taxomony terms to use in filter
- This topic has 8 replies, 3 voices, and was last updated 3 years, 11 months ago by Ross.
-
Anonymous(Private) December 15, 2020 at 4:31 am #269811
I’m looking to create a dropdown using an existing taxonomy, the issue I’m facing is that the taxonomy currently has a large number of items and I only want maybe 5 of them to appear in the dropdown.
I tried using the include/exclude option which allows me to create the correct dropdown, but I realised that it prevents a lot of the necessary results from showing as it excludes the selected taxonomies from the results also.
Any advice on how I could achieve this would be a huge help!
Thank you!
Trevor(Private) December 15, 2020 at 11:56 am #269854Do you have a specific (known) number of taxonomies you wish to show in the form field? If you do, it might be best to use this filter to make a custom list:
https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object
Anonymous(Private) December 21, 2020 at 8:40 am #270444Awesome, thanks Trevor! This looks like it’s definitely on the right track! I’ve tried using the filter with the following foreach code and while it does remove all the unwanted terms, it doesn’t display all the wanted terms. At the moment it’s set to display as a checkbox list, if I change it to a dropdown it works as expected, but I need it to work with the checkbox format. Any idea why it doesn’t?
foreach($input_object['options'] as $key => $option) { if ( !in_array( $option->value, array(‘option-a’,‘option-b’,‘option-c’) ) ) unset($input_object['options'][$key]); }
Trevor(Private) December 21, 2020 at 2:30 pm #270534I can see the quote amrk are worng, it should be:
foreach($input_object['options'] as $key => $option) { if ( !in_array( $option->value, array('option-a','option-b','option-c') ) ) unset($input_object['options'][$key]); }
What is the full code in use?
Anonymous(Private) December 22, 2020 at 12:03 am #270642The full code is below. They both work as dropdowns, but neither work correctly as checkboxes. They output some options, but not all of them. It might be to do with how I removed them from the array, is there a better way to do it?
function filter_input_object($input_object, $sfid) { if( !in_array( $input_object['name'], array('_sft_age_group','_sft_health_problem') ) ) return $input_object; if( $input_object['name'] == '_sft_age_group' ) { foreach($input_object['options'] as $key => $option) { if ($option->value == '') { } elseif ($option->value == 'prenatal') { $option->label = 'Early Childhood (<5 year)'; $option->value = 'prenatal+infant+pre-school'; } elseif ($option->value == 'primary-school') { $option->label = 'Primary School (5-12 years)'; } elseif ($option->value == 'early-secondary-school') { $option->label = 'Secondary School (13-17 years)'; $option->value = 'early-secondary-school+late-secondary-school'; } elseif ($option->value == 'early-adulthood') { $option->label = 'Young Adulthood (18+ years)'; } else { unset($input_object['options'][$key]); } } return $input_object; } if( $input_object['name'] == '_sft_health_problem' ) { foreach($input_object['options'] as $key => $option) { if ( !in_array( $option->value, array('option-a','option-b','option-c') ) ) { unset($input_object['options'][$key]); } } return $input_object; } } add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
Ross Moderator(Private) December 23, 2020 at 4:54 pm #270963Hi Jessica
I think I see what is happening!
So I just tried your code (albeit changed it for my setup) and the strangest thing was happening, I was getting some checkboxes with labels missing, and some working just fine.
I then realised (and didn’t know) that at least for checkboxes, the array of
options
must be in order.So unsetting specific values leaves “holes” in the array, that for some reason our checkbox code can’t work around for now.
Instead I did this (push the wanted options, into a new array):
function filter_input_object($input_object, $sfid) { if( !in_array( $input_object['name'], array('_sft_pa_color') ) ) { return $input_object; } if( $input_object['name'] == '_sft_pa_color' ) { $new_options = array(); foreach($input_object['options'] as $key => $option) { if ( in_array( $option->value, array( 'gray', 'green') ) ) { $new_options[] = $option; } } $input_object['options'] = $new_options; } return $input_object; } add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
Another way would be to keep your code exactly the way it was, but before returning
$input_object
do:$input_object['options'] = array_values( $input_object['options'] );
This will effectively reset the indexes too.
Let me know how you get on!
Thanks
Ross Moderator(Private) December 28, 2020 at 5:44 pm #271182Great stuff, glad you are setup 🙂
Best
-
AuthorPosts