Home › Forums › General Issues › get_field return empty implementing API
I created a new taxonomy “quote_category” and added 2 customs fields “category_image” and “category_color”.
trying to fetch and return these values inside my REST API but only get null.
[
{
“id”: 12,
“name”: “Fulfillment”,
“description”: “”,
“image”: null,
“color”: null
},
code:
function wl_quotecategory() {
$terms = get_terms( array(
'taxonomy' => 'quote_category',
'hide_empty' => false,
'fields' => 'all',
) );
$data = [];
$i = 0;
foreach($terms as $term) {
// get the current taxonomy term
$image = get_field('category_image', $term->term_id);
print_r($image);
$data[$i]['id'] = $term->term_id;
$data[$i]['name'] = $term->name;
$data[$i]['description'] = sanitize_text_field($term->description);
$data[$i]['image'] = the_field('category_image', $term->term_id);
$data[$i]['color'] = the_field('category_color', $term->term_id);
$i++;
}
return $data;
}
I’m creating a post via the REST API. On the frontend, get_field_object() returns empty until I go into wp-admin and save a value for an ACF field …
it works for me with the POST type, I’m having problems with TEMRS.
yes, my term object `{
“id”: 12,
“name”: “Fulfillment”,
“description”: “”,
“image”: null,
“color”: null
}`
has an image and color value assigned from the wp-admin
any idea to solve?
made it work using this code instead
$cat_lookup_key = 'quote_category_'.$term->term_id;
$data[$i]['image'] = get_field('category_image', $cat_lookup_key);
$data[$i]['color'] = get_field('category_color', $cat_lookup_key);
$data[$i]['priority'] = get_field('category_priority', $cat_lookup_key);
hope it helps someone else.
You must be logged in to reply to this topic.
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.