Support

Account

Home Forums General Issues get_the_terms() don't retrieve custom fields

Helping

get_the_terms() don't retrieve custom fields

  • Hi,

    i have a custom taxonomy named “Contact”
    For this taxonomy, i have 2 custom fields named “Email du contact” and “Télephone du contact”

    I can’t retrieve those 2 custom fields

    I try to do this :

    $terms = get_the_terms( get_the_ID(), 'contact' );

    But when i do a print_r, i have my taxonomies without those 2 custom fields :

    Array
    (
        [0] => WP_Term Object
            (
                [term_id] => 127
                [name] => Contact 1
                [slug] => contact-1
                [term_group] => 0
                [term_taxonomy_id] => 48
                [taxonomy] => contact
                [description] => 
                [parent] => 0
                [count] => 5
                [filter] => raw
            )
    
        [1] => WP_Term Object
            (
                [term_id] => 130
                [name] => Contact 2
                [slug] => contact-2
                [term_group] => 0
                [term_taxonomy_id] => 51
                [taxonomy] => contact
                [description] => 
                [parent] => 0
                [count] => 1
                [filter] => raw
            )
    
    )

    Contact taxonomy

    Thanks for your help

  • You need to loop the taxonomies and in doing so, get the taxonomy ID

    Once you have that, you can access the custom fields you store against the taxonomy.

    Something like:

    <?php if( $contacts = get_terms( array( 'taxonomy' => 'contact' ) ) ) :
    foreach( $contacts as $contact ) : 
    	$email_du_contact  = get_field('email_du_contact', 'term_' .$contact->term_id  );?>
    	<?php echo $email_du_contact; ?>
    <?php endforeach;      
    endif;
    ?>

    Code untested

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

You must be logged in to reply to this topic.