0

I am attempting to feed term meta values from my custom taxonomy, "firm"/"firms", on to a User profile page at author.php.

Users are attached to this taxonomy thanks to the LH User Taxonomy plugin. I need to display term meta fields for the firm to which s/he belongs.

So far, I have managed to find wp_get_object_terms useful. This code echos the slug and name meta of the taxonomy term; description also works...

      $firm_terms = wp_get_object_terms( $curauth->ID,  'firm' );

      if ( ! empty( $firm_terms ) ) {
        if ( ! is_wp_error( $firm_terms ) ) {
                foreach( $firm_terms as $term ) {
                    echo '<a href="' . get_term_link( $term->slug, 'firm' ) . '">' . esc_html( $term->name ) . '</a>';
                }
        }
      }

It works, it seems.

However, I have only managed to output those three standard term meta fields - name, slug and description. In addition, I also have a couple of dozen other term meta fields thanks to putting an Advanced Custom Fields field group on the Edit User page, storing extra "firm" details. These are intended for output but they do not show, using code like this (fragment):

      foreach( $firm_terms as $term ) {
        echo $term->firm_social_twitter_handle;
      }

Is wp_get_object_terms only built to show name, slug and description? Is there a way I can also echo out fields like firm_social_twitter_handle and firm_geo_city?

Do I need to use a combination of that and get_terms, or something else? I had earlier found some success using get_terms, but couldn't manage to constrain it to only take meta from the taxonomy term to which the User is attached, like I can using the above wp_get_object_terms code.

1
  • The extra details are attached to the term or do you mean are attached to an user?
    – dbeja
    Jul 14, 2017 at 8:28

1 Answer 1

1

If those extra fields are saved on Edit User page you should get them with get_user_meta function:

$social_twitter_handle = get_user_meta( $curauth->ID, 'firm_social_twitter_handle', true );

If those extra fields are attached to each term you can use get_term_meta function:

$firm_terms = wp_get_object_terms( $curauth->ID,  'firm' );

if ( ! empty( $firm_terms ) ) {
    if ( ! is_wp_error( $firm_terms ) ) {
            foreach( $firm_terms as $term ) {
                echo '<a href="' . get_term_link( $term->slug, 'firm' ) . '">' . esc_html( $term->name ) . '</a>';
                $firm_social_twitter_handle = get_term_meta( $term->term_id, 'firm_social_twitter_handle', true );
                echo $firm_social_twitter_handle;
            }
    }
}
6
  • dbeja, the fields are in taxonomy terms. I presume I prefix that foreach code with $firm_terms = wp_get_object_terms( $curauth->ID, 'firm' );? Now I have the reverse problem - whilst I can echo out fields like firmsocial_twitter_handle, I am unable to find the standard name, slug and description using the same method. Jul 14, 2017 at 9:51
  • No, you keep what you had for the other fields. This was just to get those extra fields. What function are you using? get_user_meta or get_term_meta?
    – dbeja
    Jul 14, 2017 at 9:53
  • I changed the code with your example
    – dbeja
    Jul 14, 2017 at 9:58
  • get_term_meta, as that's the info I want to echo out (albeit on to an author.php whose use is under this taxonomy). From your example, I now see the difference between echoing the standard name term value and an extra field like firm_social_twitter_handle - whilst you have to find firm_social_twitter_handle using get_term_meta, name (and slug and description) are available much more easily, using $term->name. Sounds like name, slug and description have more a value than a meta. Is that fair to say? Jul 14, 2017 at 10:09
  • Yes. What happens is that name and slug are columns of the same table of terms. When you get a term by ID you have in the same row name and slug, they always exist. Other fields are custom fields and can be whatever you want and any number of them, so they are saved in a different database table with a relation on the term ID. You can check WP database: codex.wordpress.org/Database_Description
    – dbeja
    Jul 14, 2017 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.