Support

Account

Home Forums General Issues Field group at taxonomy does not work Reply To: Field group at taxonomy does not work

  • Ok, I’ve done a bit of testing and I agree that something is not working as expected… until I figured out how to get taxonomy fields. I’ll try and lay this out so that you guys can tell me if I’m doing the right thing!

    Steps to reproduce:

    1. Add a new Field Group: /wp-admin/post-new.php?post_type=acf
    2. Add a new field to this group
    3. Set the Location Rules: Show field group if Taxonomy Term is equal to [taxonomy], Save field group
    4. Add a new taxonomy term with the custom field: /wp-admin/edit-tags.php?taxonomy=[taxonomy]
    5. Attempt to retrieve field in theme with get_field()

    I was unable to get the field data properly until realizing that I wasn’t calling the taxonomy field properly.

    http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-a-taxonomy-term/

    So getting the field data looks like this (using @mdetoni’s taxonomy):

    $terms = get_terms( 'audiocategorias', array( 'hide_empty'=>false ) );
    foreach($terms as $term){
    	$field = get_field('ab_rolp', $term->taxonomy.'_'.$term->term_id)
    	echo '<pre>field: '.print_r( $field, true).'</pre>';
    }

    To get taxonomy fields, the post ID variable must start with taxonomyname_, so in this case audiocategorias, or in the example we pull it directly from the taxonomy term itself: $term->taxonomy.'_'.

    Does this help anyone or am I heading in the wrong direction?

    Edit: @drift2’s example

    $terms = get_terms( 'radioroom', array( 'hide_empty'=>false ) );
    foreach($terms as $term){
    	$field = get_field('style_id', $term->taxonomy.'_'.$term->term_id)
    	echo '<pre>field: '.print_r( $field, true).'</pre>';
    }

    Note that in these examples we are using 'hide_empty'=>false in order to force the retrieval of terms. This is required for a term to show up in our list if there aren’t any posts with this term selected.