Support

Account

Home Forums ACF PRO Add Custom Tax To Relationship Field?

Solved

Add Custom Tax To Relationship Field?

  • Is it possible to list the custom tax the post is assigned to in the relationship list?

    As currently you can only list the posts name and its featured image.

    I need it to show which custom tax its assigned to as well.

    I know its using something like this but unsure how to pull it in../

    function my_relationship_result( $result, $object, $field, $post ) {
    
        // load a custom field from this $object and show it in the $result
        $custom_tax = tax here????;
    
        $result .= '[' . $custom_tax .  ']';
    
        return $result;
    
    }
    
    // filter for every field
    add_filter('acf/fields/relationship/result', 'my_relationship_result', 10, 4);
  • You’re question is a little confusing for me. Do you mean that you want to show the terms in a custom taxonomy that the poas is assigned to? If it is, what is the slug for the custom taxonomy?

    Doing this would look something like

    
    function my_relationship_result( $result, $object, $field, $post ) {
        // get post terms
        $args = array(
            'fields' => 'names'
        );
        $terms = get_post_terms($post->ID, 'your-taxonomy-slug', $args);
        if ($terms) {
            $result .= '[' . implode(', ', $terms) .  ']';
        }
        return $result;
    }
    
    // filter for every field
    add_filter('acf/fields/relationship/result', 'my_relationship_result', 10, 4);
    
  • Thank you John, but that brings up an error.

    Basically i want the relationship field to show the post name as well as the custom tax its assigned too.

    I tired your code but got an unexpected end error code.

  • My code is missing a semicolon at the end of the last line. I edited it.

    But you’ve still got me confused.

    Basically i want the relationship field to show the post name as well as the custom tax its assigned too.

    Do you mean the terms assigned? The custom tax that’s assigned does not make much sense.

    Also you will still need to edit my code to provide the slug for you custom taxonomy, that’s if you are looking to show the “TERMS” of the post.

  • Sorry yes.

    So currently the relationship selection box shows the lists of posts and their featured image, i would like it to show the custom taxonomy name that post is assigned to.

    So if the post is assigned to the custom tax cat 1 and cat so, they are showed along side its name.

    Does that explain it better?

  • The code that I posted will work if you supply your custom taxonomy slug in this line

    
    $terms = get_post_terms($post->ID, 'your-taxonomy-slug', $args);
    
  • Thank you John, unfortunately its not working.

    The custom tax is called ‘area’ which has two categories within.

    When try your code with ‘area’ put in ‘your-taxonomy-slug’ i get a fatal error and the admin page doesn’t load completely..


    Fatal error: Call to undefined function get_post_terms() in functions/inc/acf.php on line 71

    Line 71 is..

    $terms = get_post_terms($post->ID, 'area', $args);

  • Try this

    
    function my_relationship_result($title, $post, $field, $post_id) {
      $args = array(
        'fields' => 'names'
      );
      $terms = wp_get_post_terms($post->ID, 'area', $args);
      if ($terms) {
        $title .= ' ['.implode(', ', $terms).']';
      }
      return $title;
    }
    add_filter('acf/fields/relationship/result', 'my_relationship_result', 10, 4);
    
  • Perfect thank you 🙂

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

The topic ‘Add Custom Tax To Relationship Field?’ is closed to new replies.