Home › Forums › General Issues › 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:
http://www.advancedcustomfields.com/resources/functions/get_field_object/
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
Thanks
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:
<?php
$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;
?>
<?php
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: http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-a-taxonomy-term/
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):
<?php
$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
The topic ‘WordPress Advanced Custom field get_field_object('field_name') don't work’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.