Home Forums Backend Issues (wp-admin) Relationship field: hide empty terms…..


Relationship field: hide empty terms…..

  • 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;

  • Your question is confusing. A relationship field does not return terms. Are you talking about a taxonomy field?

  • 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…..

  • It’s never a good idea to alter the plugin code, even if it works, unless you want to make the same edits every time a the plugin is updated. This is why you use filters and actions in WP.

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

You must be logged in to reply to this topic.