Forums Forums Search & Filter Pro Select specific taxomony terms to use in filter

Viewing 9 posts - 1 through 9 (of 9 total)
  • Anonymous
    #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
    #269854

    Do 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
    #270444

    Awesome, 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
    #270534

    I 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
    #270642

    The 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);
    Trevor
    #270654
    This reply has been marked as private.
    Ross Moderator
    #270963

    Hi 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

    Anonymous
    #271138

    Yayyyy! Thank you so much Ross, I tried out both options and they work perfectly!

    Thank you both for all your help with this, I really appreciate it!

    Ross Moderator
    #271182

    Great stuff, glad you are setup 🙂

    Best

Viewing 9 posts - 1 through 9 (of 9 total)