get_fields() is meant for getting all the fields of a Post, but what about a function for getting all of the custom fields for a taxonomy?
It will work for a taxonomy, just use the right ID
$fields = get_fields("term_{$term_id");
Unfortunately, that’s not my result. I’m getting false
. Here’s my code that isn’t working. I’m trying to use the WP REST API to return custom fields on a custom Taxonomy called ‘region’ on a custom Post Type.
function regions_get_custom_fields_cb($object, $field_name, $request){
$thing = $object['slug'] . "_" . $object['id'];
return get_fields($thing);
}
add_action('rest_api_init', function(){
register_rest_field('region', 'custom_data',
array(
'get_callback' => 'regions_get_custom_fields_cb',
'update_callback' => null,
'schema' => null
)
);
});
The code I supplied was strictly for use in a template. I’m not going to be able to help you with doing this through the rest api. I simply do not know enough about it. If this was something I had to do this I would start here https://github.com/airesvsg/acf-to-rest-api
After much scrutiny and a much needed lunch break, I found that the reason it returned false was $thing
needed to be the following:
$thing = $object['taxonomy'] . "_" . $object['id'];
My mistake! I was returning the slug of a specific ‘region’ instead of literally the taxonomy/term slug.
I accepted your answer, although there is a typo with a missing ‘}’