Support

Account

Home Forums Backend Issues (wp-admin) Select taxonomy by name instead of id in Relationship field

Solved

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.

  • Hi,
    could you please post all solution ?

    thanks

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

The topic ‘Select taxonomy by name instead of id in Relationship field’ is closed to new replies.