Forums Forums Search & Filter Pro Displaying Description instead of Name for Custom Taxonomy in Dropdown?

Tagged: 

Viewing 3 posts - 1 through 3 (of 3 total)
  • Trevor
    #251425

    It is possible, but only with custom PHP coding that you (or a third party coder you would hire) would have to do.

    You would have to use this filter in your child theme function.php file:

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    You would use PHP to fetch an array of ALL the descriptions and terms, and then step through the field options replacing each in turn.

    Anonymous
    #262498

    While this thread is a few months old, here is the code for my solution used for a current project. The code below gets the terms for the taxonomy called ‘commodity_codes’ and then updates the Search and Filter field ‘_sft_commodity_codes’ in my search forms by leaving the value intact for the query and simply updating the label to be a concatenation of the term name and the first 21 characters of the description with camel casing.

    Thanks to the gist here: https://gist.github.com/rmorse/7b59b45a14b1ca179868

    function filter_function_name($input_object, $sfid)
    {
    	if($input_object['name']=='_sft_commodity_codes')
    	{
    		//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;
    		}
    		
    		//get the terms for the taxonomy
    		$terms = get_terms('commodity_codes');
    		
    		//now we know there are options we can loop through each one, and change what we need
    		foreach($input_object['options'] as $option)
    		{
    			foreach($terms as $term)
    			{
    				if($term->name==$option->value)
    				{//we want to change the label for the option "black" - we can feed back in the count number to the label for this field type
    					$option->label = $term->name . ' : ' . ucwords(substr($term->description,0,21));
    				}
    			}
    		}
    	}
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);

    Hope this helps others.

    — Tom @NPCrowd : https://npcrowd.com

    Trevor
    #262513

    Thanks for sharing Tom. I know others will find that useful. I will close this thread for now, as it is old.

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