5

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:

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

where basic - name for my taxonomy. But this doesn't work.

Any suggestions?

closed as off-topic by fuxia May 16 '16 at 18:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    get_field is not a WordPress core function, your question appears to be missing some information. – Milo Nov 3 '13 at 19:29
  • get_field, this Advanced Custom field function, i use ACF plugin – John Genry Nov 3 '13 at 19:46
  • have you read the documentation for get_field? – Milo Nov 3 '13 at 19:58
  • also How to get values from a taxonomy term – Milo Nov 3 '13 at 19:59
  • ok, try: '<?php $variable = get_field('my_cf', 'basic'); echo $variable; ?>' and nothing, where - my_cf - custom field, basic - taxonomy in post type - and my_cf = 250 value, but there is no outputб there may be specific output custom field value in taxonomy? – John Genry Nov 3 '13 at 20:11
3

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' );
1

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;
  • ok, my url: edit-tags.php?taxonomy=basic&post_type=products how i can get id taxonomy? in basic custom field - my_cf, '$variable = get_field( 'my_cf', 'basic_42' ); - not working – John Genry Nov 4 '13 at 10:45
  • When I answered this question I didn't notice you were using a plugin to create the custom terms. Sticking with however ACF works may be your best bet. If that's not working for whatever reason though you can get the term id with get_term_by(). Documentation: codex.wordpress.org/Function_Reference/get_term_by – Joey Yax Nov 4 '13 at 18:08
1

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!

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