Exist there a hook to change the tax query for the relationship field? This was in some old threads dicussed but only for the resulting query – not for the list in the dropdown.
The workaround would be to get all terms at “load_field” and and this to the taxonomy array – but it would be more easy and more quick to use a hook – or perhaps with an update of the field there should be an option
'hide_empty' => true;
The relationship field shows “if activated” all taxonomies with their corresponding terms objects. But it shows ALL terms. Therefore my question. The first try was to define taxononomies in the PHP section for these fields but i have to define not the taxonomy but taxonomy:term for every allowed selection. This is not very handy. I could generate my own list and push it in the “load_field” section but it would be more useful to have an action or filter to hide the empty TERMS – or if all terms are empty the corresponding taxonomy.
ACF does not have any filters for alter the taxonomy/term list shown in a relatiohship field.
You would have to use a pre_get_terms filter
To apply this filter to only the field being displayed your would use the acf/render_field action and you would have 2 actions, one that happens before acf renders the field to add your filter and one after acf renders the field to remove your filter.
@hube2 thanks for your idea but this is all a very “unbeautiful” solution but it would work – i have tested with “tweaking” the relationship a little to add “hide_empty” true and tested it. But now i found a bug and opened a support case:
I found in line includes\libraries\acf\includes\fields\class-acf-field-relationship.php on line 421: $terms = acf_get_grouped_terms( $term_args );
and i followed the function to api-term.php to line 171. In line 181 i changed ‘hide_empty’ to true and give it a try -> it works BUT here seems to be a BUG:
all taxonomies and their corresponding terms are shown (which are not empty). BUT 2 taxonomies (screenshot) are shown because there is in each one “filled term” but not the term itself. I think the problem is the “same” taxonomie name like für posts. but its only the “label” the same not the taxonomy name. It is “dpf_work_category” and “dpf_work_type” – i think there is the bug.
I changed the registered name from one taxonomy from “dpf_work_category” to “dpf_work_category2” and than the missing term is shown under the category name. SO i think its a bug while checking the taxonomy name against the wordpress built-in category names…..
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!
📣 “ACF Chat Fridays”— Advanced Custom Fields (@wp_acf) January 31, 2023
The ACF team holds their first open office hours this Friday! Come and talk ACF, and ask questions about building sites with the plugin.
We’d love to see you there!
📆 Friday 3rd Feb - 3pm UTC
👉 Register here - https://t.co/3UtvQbE4CU pic.twitter.com/oTwW9K1XQ0
© 2023 Advanced Custom Fields.