Taxonomy term select by foreign characters
-
Hello, I have a problem with the field called Taxonomy Terms (with all children) and the field type Select (multiple terms). I have Swedish language characters inside my taxonomy terms like ö and ä. If I type any of these characters, StylizedUI (select2) gives me also o and a which I dont want. By the way, ACF field Taxonomy with Multiple select field type gives me right ordering with using ö and ä. Any suggestions?
-
Hello,
Thanks for the feedback! I ran some tests, and it looks like that’s the default behavior of the Select2 library (which is used by ACF & ACF Extended).
Here is a video example with a native ACF Select field, with stylized UI enabled (Select2) and the following choices: Malo, Malö: https://i.imgur.com/rjR5mAL.mp4
The letter “o” matches “o” and “ö”, and vice versa. Select2 can be probably tweaked to find the exact matching term/character, maybe you should dig around on Stackoverflow or Github. To be honest I don’t have much time right now do it myself, ACF Extended is already improving/enhancing WordPress and ACF, that’s already a lot of work to keep everything compatible. I’m not sure that ACF Extended should also improve external libraries like Select2 JS, which would require additional maintenance.
A perfect match could also potentially break other stuff or features (Select2 is used in many places in ACF), so I’m not really sure that it should be added in ACF Extended. If you find a solution just let me know tho, I’ll gladly take a look.
Hope it helps!
Regards.
Hello again,
But it’s strange that if I’m using Taxonomy field with multiple select display option within ACF Pro, it does give me exact terms with ö and ä, not terms with o and a. Of course, posts table collation in the MySQL database must be set to utf8mb4_swedish_ci.
-
This reply was modified 5 years, 8 months ago by
tomsim.
Hello,
This is due to the fact that the ACF Taxonomy field use “Ajax to lazy load choices” setting as builtin setting. If you enable this setting in the ACF Select field or in the ACF Extended Taxonomy Terms field, it will correctly match the special character (I had to use
utf8mb4_swedish_cicollation in the DB).See video: https://i.imgur.com/eX6QZsU.mp4
The Ajax method use PHP to match the search request (which use strict
===comparison). When disabled, Select2 is using its native JS method to match the search request, which looks like to not be strict.Hope it helps!
have a nice day.
Regards.
Hello,
OK, finally got it. But now another problem arises. Seems that “ajax lazy load choices” change the hierarchical ordering of select field terms. Now the most likely term is on top of the select box without it’s parent term. Without “ajax lazy load choices” it gave me most likely term between it’s parent and children if they ALL consisted part or the whole of the term I typed in. Why is it so? The same behavior is with ACF Pro, too.
Hello,
I don’t understand the problem. Can you please provide context or screenshots? The lazy load choices use the native ACF way to load choices using Ajax.
Regards.
Here are the screenshots.
Entire hierarchy: https://www.screencast.com/t/FQpAYiTm
With “ajax lazy load choices”: https://www.screencast.com/t/5GGcizCTWqU8
Without “ajax lazy load choices”: https://www.screencast.com/t/Zc1yft7oqozs
The last ordering is what I want to achieve.
Regards,
tomHello,
In the “With Ajax” screenshot there isn’t any search request, so it’s hard to understand what you want to achieve by comparing with the “Without ajax” screenshot.
The ACF Taxonomy field results are probably the same as both ACF Taxonomy & ACFE Taxonomy Terms use the same method to find terms, using the
searchargument in theWP_Term_Query. See documentation.The results aren’t ordered by hierarchy, but by relevance returned by the WP Query.
Regards.
Hello,
Just updated second picture. Difference is that on the “with ajax” pic the term “30” is on the top, and on the pic “without ajax” the term “30” is on the bottom. If this behavior is “by design”, so be it. I just have to come along without ajax in my field settings and give up strict ö and ä search.
Best regards,
Hello,
Okay I see the difference now. Like I explained, this is due to the
WP_Term_Queryresults which are ordered by revelance by default.When disabling the Ajax Lazy Load, the Select2 JS is simply filtering the list which is already loaded in the page, using the default order (by terms hierarchy). This is why
Saab 30result is returned first, because it is already loaded higher than its childs.If you use the ACF Taxonomy field, there is a way to get a similar search results, using the following hook:
add_filter('acf/fields/taxonomy/query/name=my_taxonomy_field', 'my_taxonomy_query', 10, 3); function my_taxonomy_query($args, $field, $post_id){ $args['orderby'] = 'parent'; return $args; }This filter is used to change the arguments right before it is sent to
WP_Term_Queryand tells to order results by termsparent.Video of the result: https://i.imgur.com/4DQA2cj.mp4
Unfortunately there is no such hook for the ACFE Taxonomy Terms field right now, but I’ll definitely add it in the next patch!
Added to the Trello Board: https://trello.com/c/PDZq0LAy/403-taxonomy-terms-add-hooks-to-filter-wptermquery-arguments-terms-result-title
Regards.
Hello,
I tried to use this filter, but the result is wrong: https://www.screencast.com/t/pQ2ji3IvjcD
30needs to be underSaab 300not underSaab 30Regards.
Hello,
I understand. Doing a SQL search while keeping the hierarchical order would be too complicated to achieve using one
WP_Term_Query. A workaround would be to make a custom query inside that hook (before ACF makes its own query), list all terms, re-order it hierarchically, search within term names, and force the newWP_Term_Queryto use our filtered list with theincludeargument.Here is a code example:
add_filter('acf/fields/taxonomy/query/name=my_taxonomy_field', 'my_taxonomy_query', 10, 3); function my_taxonomy_query($args, $field, $post_id){ // Bail early if not a search requrest if(!isset($args['search'])) return $args; // Clone args $_args = $args; // Remove search from clone unset($_args['search']); // Query (retrieve all terms) $terms = acf_get_terms($_args); // Nothing found if(empty($terms)) return $args; // Re-order all terms hierarchically $ordered_terms = _get_term_children(0, $terms, $field['taxonomy']); if(!empty($ordered_terms)) $terms = $ordered_terms; // Loop in terms foreach($terms as $key => $term){ // Search within terms names if(stripos($term->name, $args['search']) !== false) continue; // Does not match the search request. Delete unset($terms[$key]); } // Bail early if nothing matched the search request if(empty($terms)) return $args; // Filter the list to retrieve only term ids $list = wp_list_pluck($terms, 'term_id'); // Force the new query to use our filtered list $args['include'] = $list; // Force the new query to use our order of inclusion $args['orderby'] = 'include'; // Remove search argument from the new query (it's already filtered) unset($args['search']); // return return $args; }Video result: https://i.imgur.com/FTWaY4W.mp4
It looks like that’s what you’re trying to achieve. If that doesn’t do the job, I will have to let you do your own research. I don’t have much time for more custom development, and the topic is sliding into general WordPress development questions & Terms Queries support.
Hope it helps!
Regards.
Hello,
Now it works as intended. Thank you for a very good support!
Cheers,
Hello,
I’m glad to hear it now works as you wanted.
If you enjoy this plugin and the support, feel free to submit a review, it always helps and it’s much appreciated 🙂
Have a nice day!
Regards.
-
This reply was modified 5 years, 8 months ago by
The topic ‘Taxonomy term select by foreign characters’ is closed to new replies.