Support

Account

Home Forums General Issues How to use the taxonomy/result filter?

Solving

How to use the taxonomy/result filter?

  • So at the bottom of the documentation for the Taxonomy field there is this statement:

    It is possible to customise the text displayed for each taxonomy term item. To do this, please use the filter acf/fields/taxonomy/result

    I would definitely like to find out more about this, but can’t seem to find it anywhere. Basically the situation is I am trying to work with a client’s plugin that can retrieve and display metadata (not custom taxonomies). We need to display custom taxonomies, so I would like to use ACF’s Taxonomy field to save the taxonomies as metadata, and then simply have the plugin retrieve and display it. The problem is that it will be retrieving post objects or ID’s — not the term names. The documentation for the other plugin seems somewhat poor, so I’m not sure how easy it would be to simply include a filter that would alter the output. I figured that this taxonomy/result filter looks like it is just what I need, but I’m not sure how to take advantage of it.

    Thanks.

  • For what it’s worth I found a workaround for anybody needing similar functionality. What I did was use the update_value filter to create custom meta data based on taxonomy term names. Here is a snippet of the code:

    
    function custom_acf_values(  $value, $post_id, $field ) {
    
    	// Set authors
    	if( $field['name'] == 'authors' && $value != '' ) :
    
    		$names = '';
    		$authors = $value;
    	    
    	  if( $authors ) :
    
    		  foreach( $authors as $author ) {
    
    		  	$sep = '; ';
    		  	if( $author == end( $value ) ) {
    		  		$sep = '';
    		  	}
    
    		  	$author = get_term( $author, 'authors' );
    		  	$name = $author->name . $sep;
    
    		  	$names .= $name;
    	  	}
    	  endif;
    	  update_post_meta( $post_id, 'poster-authors', $names );
    
    	endif;
    }
    add_filter('acf/update_value', 'custom_acf_values', 10, 3);
    

    However, I am still interested in learning more about the filter I mentioned in OP.

  • Hello
    I’m trying to use the acf/fields/taxonomy/result/ filter, but without any result.
    Even if I enter a simple test code like this:

    function tp_taxonomy_result( $title, $post, $field, $post_id ){
    	
    	$title = 'test text';
    	return $title;
    	
    }
    
    add_filter('acf/fields/taxonomy/result/key=field_5a046d4c673af', 'tp_taxonomy_result', 10, 4);

    I do not get any results in my taxonomy box.

    taxonomy box

    Any idea why the filter does not seem to work?

  • That filter only works when you use a select field and will not work for checkboxes or radio type field.

  • Oh thanks @hube2!
    There is no official page related to this filter in the documentation and in the pages concerning similar filters it wasn’t indicated that it worked only in the presence of a select (very strange). I hope that it will also be extended to other possibilities.

  • It is probably not documented because of this or any number of things. I only know it exists because I dig through ACF code a lot, which is the same way I just discovered why it wasn’t working for you. When showing a select field ACF uses it’s own function to list the terms, where this filter is called. When showing checkboxes and radio field ACF uses wp_list_categories() which is pretty much unfilterable. I don’t know why there’s a difference other that the developer hasn’t gotten to it.

  • I am trying to display the whole tree of the taxonomy within the select field.
    So that “parent => child => 2child” is shown, when selecting a term.
    Any idea how to accomplish this?

    Thanks very much!

  • 
    function your_function_name($title, $term, $field, $post_id) {
      $ancestors = get_ancestors($term, $term->taxonomy, 'taxonomy');
      if (!empty($ancestors)) {
        $parts = array();
        array_reverse($ancestors)
        foreach ($ancestors as $ancestor) {
          $ancestor_term = get_term($ancestor, $term->taxonomy);
          $parts[] = $ancestor_term->name;
        }
        $parts[] = $term->name;
        $title = implode(' => ', $parts);
      }
      return $title;
    }
    
Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘How to use the taxonomy/result filter?’ is closed to new replies.