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

Tagged: 

Viewing 4 posts - 1 through 4 (of 4 total)
  • Anonymous
    #251375

    I have a custom taxonomy which has numbers for names. So the items go from 0001 – 2500. I have 2500 items in the custom taxonomy.

    However, for each tag name, I also have a “description”. The descriptions are words like “forest” or “ocean”, etc. So each taxonomy items has a name like “0035” and a description like “ocean”.

    I want my users to search based on the custom taxonomy Descriptions, not the name. In the dropdown or checkbox, they only see 0001,0002,0003,etc. Bu that means nothing to them. Instead I want to display forest,ocean,mountain,beach,etc.

    How can I select to display the custom taxonomy Description instead of the Name in the search form?

    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 4 posts - 1 through 4 (of 4 total)