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: http://support.advancedcustomfields.com/forums/topic/adding-image-to-the-relational-user-select-field/.
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.
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?
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.
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.
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. https://core.trac.wordpress.org/browser/tags/4.2.2/src/wp-includes/taxonomy.php#L1899
Hope you can find a solution to extend the WP posts search and I hope to heave cleared up the difference
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2021 Advanced Custom Fields. Subscribe