Get value in custom field with taxonomy

I have custom field my_cf for Taxonomy/Term. How can I get and output value with custom field for taxonomy/term?

I’ve tried using:

Read More
$variable = get_field('my_cf', 'basic'); 
  echo $variable; 

where basic – name for my taxonomy. But this doesn’t work.

Any suggestions?

Related posts

3 comments

  1. I can’t really explain it any better than the ACF documentation page I posted in the comments:

    All the API functions can be used with a taxonomy term, however, a second parameter is required to target the term ID. This is similar to passing through a post_id to target a specific post object.

    The $post_id needed is a string containing the taxonomy name + the term ID in this format: $TaxonomyName_$TermID

    So if your custom field is my_cf, and your taxonomy name is basic (not term name) and the term ID within your taxonomy is 42, then you need:

    $variable = get_field( 'my_cf', 'basic_42' );
    
  2. Is your field data stored in wp_options? If so…

    $term_id = 12345;
    $term_meta = get_option( 'taxonomy_' . $term_id );
    $my_cf = $term_meta[ 'my_cf' ];
    echo $my_cf;
    
  3. I use CMB2 to set up custom fields, and the logic is not so different from ACF in many cases. For my specific use case I’ve created a very simple but flexible function in order to make a few checks for the taxonomy before displaying the custom field value.

    Considering one have created a custom field named my_cf for let’s say a taxonomy named basic as per your example, the following function might help answer your question and perhaps extend your custom field’s usage a bit.

    function get_taxonomy_terms_custom_fields( $taxonomy = '' ) {
    
        global $post;
    
        $terms = get_the_terms( $post->ID, $taxonomy );
    
        // Check if we have a taxonomy and that it is valid. If not, return false
        if ( !$taxonomy )
            return false;
    
        // Sanitize the taxonomy input
        $taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
    
        // keep playing safe
        if ( !taxonomy_exists( $taxonomy ) )
            return false;
    
        foreach ( $terms as $term ) {
        // Set a variable for taxonomy term_id
        $tax_term_id = $term->term_id;
    
        $my_field = get_term_meta( $tax_term_id, 'my_cf', true ); 
    
        // Make sure we do not have a WP_Error object, not really necessary, but better be safe
        if ( is_wp_error( $term ) )
            continue;
    
            // escaping the returned value // esc_html(), esc_url(), esc_attr()
    
            return esc_html($my_field);
    
        }
    }
    

    Simply use <?php get_taxonomy_terms_custom_fields ('basic'); ?> replacing basic with you own taxonomy name.

    The function get_taxonomy_terms_custom_fields () will check for the specified taxonomy and kind of loop through all categories assigned to a post, post_type and then return the custom field value if present, avoiding erros if not. It could also be extended to check for a field which produces an array() such as a repeatable field.

    I hope it helps – Good Luck!

Comments are closed.