Home Forums Feature Requests Filter for taxonomy term names


Filter for taxonomy term names

  • When using the taxonomy field it always returns the names of the terms. The only filter related to this field, acf/fields/taxonomy/query, controls the query itself but not the output. I created a new set of filters in taxonomy.php around line 138 to filter the output:

    $text = $this->get_term_title( $term, $field, $options['post_id'] );
    $text = apply_filters( 'acf/fields/taxonomy/text', $text, $term, $field, $options['post_id'], $options['s'] );
    $text = apply_filters( 'acf/fields/taxonomy/text/name=' . $field['name'], $text, $term, $field, $options['post_id'], $options['s'] );
    $text = apply_filters( 'acf/fields/taxonomy/text/key=' . $field['key'], $text, $term, $field, $options['post_id'], $options['s'] );
    // add to json
    $r[] = array(
      'id'	=> $term->term_id,
      'text' => $text,

    This passes in five arguments: the initial text, the term itself, the field, the main post_id, and the search term. It works for me when the field is set to display as a select, but the field would need to be modified so that it works in other areas. This way you could, for example, have a list of slugs instead of names. Could this please be added to core?

  • This is very similar to another thread about filtering users. I think that things like this are in the works.

    The other question is here:

    I’m sure that when Elliot works on that he will consider all of the other places that people might like these filters.

  • Sweet, I linked to this post from there so it’s cross-referenced.

  • Hi @timothy_h

    In ACF PRO (not sure what version I added the filter in), you can modify the ‘text’ of each result via the filter:

    This is how ACF uses the filter:

    // filters
    		$title = apply_filters('acf/fields/taxonomy/result', $title, $term, $field, $post_id);
    		$title = apply_filters('acf/fields/taxonomy/result/name=' . $field['_name'] , $title, $term, $field, $post_id);
    		$title = apply_filters('acf/fields/taxonomy/result/key=' . $field['key'], $title, $term, $field, $post_id);

    Hope that helps


  • Thanks! That filter works great for taxonomies, but I noticed a little inconsistency between the result filter for taxonomy fields and the one for post object fields. When I’m searching within select2, whatever I’ve appended to the result via this filter is searched and included. But with post objects the search is done before the result filter is called.

    So if I had “Some entity (P012)” in both a taxonomy field and post object field–the parenthetical would be added by the result filter–searching for “P012” would only return this result in the taxonomy field, not in the post object field. Is this expected?

  • Hi @timothy_h

    I don’t think this is a bug, but more of a coincidence.

    The select2 won’t actually search the ‘result’ string, but will search what is in the DB.

    Perhaps you are having mixed results because the post contains the string p012 within it’s content? Maybe?

    Either that or I have miss rad the issue, perhaps some screenshots will help to explain?


  • Here are screenshots of the sub-fields. I’m adding a parenthetical to the search results for the taxonomy and post object fields. In the taxonomy field, you can see the underlining indicating that the parenthetical was included in the search term. This is not happening for post objects.

  • Hi @timothy_h

    Is it possible that the taxonomy term in question contains the string ‘133’ somewhere in it’s description, slug, name or any other DB data?

  • Yes, the id that gets pulled in as a parenthetical is the slug of a taxonomy term. But the parenthetical on the post object is the post_name of the post.

  • Hi @timothy_h

    Thanks mate. Interesting…

    Perhaps WP does not search the wp_posts.post_name column, but it does search the wp_terms.slug?

    I’ll do some reasearch

  • Hi @timothy_h

    Yep, looks like WP only searches the post_title and post_content.

    You may need to hook into a filter to add a custom search to the SQL string. I believe WP contains some filters to modify the SQL, but I’m not sure off the top of my head.

    The get_terms function seems to search for name and slug, so this explains the difference.

    Hope you can find a solution to extend the WP posts search and I hope to heave cleared up the difference


  • That behavior seems inconsistent; posts search titles and content, but terms search titles and slugs. I’ve filed a ticket with core to make this more consistent. I’ll post if anything changes. Thanks a lot, man.

  • Hi @timothy_h

    Nice one!

    Thanks mate

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

The topic ‘Filter for taxonomy term names’ is closed to new replies.