Home Forums General Issues WordPress Advanced Custom field get_field_object('field_name') don't work


WordPress Advanced Custom field get_field_object('field_name') don't work

  • I’m working with Advanced Custom Fields and Taxonomys in WordPress. I have a group of ACF called Person. Inside there’s several fields. One of them is called “hair_color”. So when I’m at the taxonomy-page.php the page shows the Tax info. I retreive the info this way correctly:

    $fieldf = get_field(‘hair_color’, $taxonomy . ‘_’ . $term->term_id);
    $field = get_field_object(‘field_51ac9d333d704’);
    $pelo = $field[‘choices’][ $fieldf ];
    But I have many environment (production, development), so is annoying to change ‘field_51ac9d333d704’ (field_key) that corresponds to the different databases.

    In the documentation:

    Says that the info can be retreived: $field = get_field_object(‘hair_color’) (with the field_name as parameter) But it doesn’t works, only works this way $field = get_field_object(‘field_51ac9d333d704’);

    Thanks in advance

  • $field = get_field_object('hair_color');
     $value = get_field('hair_color');
     $label = $field['choices'][ $value ];
      echo '<p>' . $label . '</p>' ;

    $label should display the hair color.

  • Hi,

    Thank you, but I get an empty value.

    a var_dump($field) is empty too.
    This is the return:

    array(17) { [“key”]=> string(16) “field_hair_color” [“label”]=> string(0) “” [“name”]=> string(10) “hair_color” [“type”]=> string(4) “text” [“order_no”]=> int(1) [“instructions”]=> string(0) “” [“required”]=> int(0) [“id”]=> string(20) “acf-field-hair_color” [“class”]=> string(4) “text” [“conditional_logic”]=> array(3) { [“status”]=> int(0) [“allorany”]=> string(3) “all” [“rules”]=> int(0) } [“default_value”]=> string(0) “” [“formatting”]=> string(4) “html” [“maxlength”]=> string(0) “” [“placeholder”]=> string(0) “” [“prepend”]=> string(0) “” [“append”]=> string(0) “” [“value”]=> bool(false) } 3


  • It looks like you are using a text field for the hair_color field rather than a select/option field.

    So try any of these:

    $queried_object = get_queried_object(); 
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;  
    $hair_color = get_field('hair_color', $taxonomy . '_' . $term_id);
    echo $hair_color;
    global $post;
    $taxonomy = "someting";
    $terms = get_the_terms($post->ID, $taxonomy);
    if( !empty($terms) )
    	$term = array_pop($terms);
    	$custom_field = get_field('hair_color', $taxonomy . '_' . $term->term_id );
    	// do something with $custom_field

    See this example for more info:

    If you do want to use a Radio Button field type and have them choose from a list of hair colors, then this might work too (I think):

    $field = get_field_object('hair_color', $taxonomy . '_' . $term->term_id);
    $value = get_field('hair_color');
    $label = $field['choices'][ $value ];
      echo '<p>' . $label . '</p>' ;

    Probably didn’t work before due to being text field rather than select/radio field and the $taxonomy value not being defined, although not sure…

  • Ok, it works that way:`
    $fieldf = get_field_object('hair_color', $taxonomy . '_' . $term->term_id);
    $field = get_field('hair_color', $taxonomy . '_' . $term->term_id);
    $value = $fieldf['choices'][ $field ];
    echo $value`

    Thank you so much for your help

  • Sorry Nuro, I have another question.

    I want to get all the values of the field ‘hair_color’.
    Is in a generic page to make a combobox to use it as a filter, so I need all the values. I don’t have the term-id value.

    Thanks in advance

  • // vars
    $queried_object = get_queried_object();
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;

    // load thumbnail for this taxonomy term (term object)
    $thumbnail = get_field(‘thumbnail’, $queried_object);

    // load thumbnail for this taxonomy term (term string)
    $thumbnail = get_field(‘thumbnail’, $taxonomy . ‘_’ . $term_id);

    Not working on my end

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

The topic ‘WordPress Advanced Custom field get_field_object('field_name') don't work’ is closed to new replies.