Home › Forums › Backend Issues (wp-admin) › Select taxonomy by name instead of id in Relationship field
Hey!
I can’t select a taxonomy term name as query criteria for a Relationship field, since it uses id’s in the plugin:
foreach( $taxonomies as $k => $v )
{
$options['tax_query'][] = array(
'taxonomy' => $k,
'field' => 'id',
'terms' => $v,
);
}
(core > fields > relationship.php:303-310)
Changing ‘id’ to ‘name’ in the plugin and e.g. ‘categories:2’ to ‘categories:Guests’ in functions.php works, but would require changing the plugin after every update. Is there a better way to do this?
I want to make this work, because although the name of the term will stay the same (registered in functions.php, just like my custom fields), the id can change from environment to environment (e.g. dev vs. production).
I hope this is placed correctly, because it could also be a feature request, but I’m not sure.
Meta: I also opened a support ticket about this, because I didn’t receive an email with a password set link. Resetting the password afterwards with the username I wanted did work though, which is why I’m able to post here now. Will open a ticket about that issue.
I doubt that this is a change the developer will make. The database only stores the term ID values for one and as a general rule, term names can be easily changed in WP.
I would build a function that gets the term ID that I’m looking for… get the terms in the taxonomy, look for the term and return the ID. Then I’d create an acf/load_field filter http://www.advancedcustomfields.com/resources/acfload_field/ and alter the taxonomy argument of the field. You can see how a relationship field is put together by creating on and then exporting the code. Any field argument that is shown when exporting a field this way can be altered during a load field filter.
I can’t help you with any login or other issues. This is a user support, users helping users. The ticket system is handled separately.
Thanks @hube2! 🙂
I doubted that, too.
Using `$term_guests = get_term_by(‘name’, ‘Guest’, ‘categories’);
$term_id_guests = $term_guests->term_id;`
solved my problem. I didn’t really follow the part about acf/load_field, but I put the code in functions.php after registering the term and before registering the ACF fields, so I think there is no need (especially since it works).
Of course, I understand, that’s why I said it was meta.
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!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.