I have a term field created with types plugin and attached to the “category” taxonomy. I need to get this field in the api rest. You cant get the termmeta in types plugin like this
types_render_termmeta($slug_term, array("term_id" => $term_id));
I don’t know how to get current category id in register_rest_field
function.
register_rest_field( 'category', 'color', array(
'get_callback' => function() {
$term_id = "DONT KNOW HOW TO GET IT";
$color = types_render_termmeta('color-de-categoria', array("term_id" => $term_id));
return $color;
}
));
Thanks in advanced.
1 Answer
The get_callback
part is generated within the WP_REST_Controller::add_additional_fields_to_object()
method with:
$object[ $field_name ] = call_user_func(
$field_options['get_callback'],
$object,
$field_name,
$request,
$this->get_object_type()
);
That means the callback has four input arguments:
'get_callback' => function ( $object, $field_name, $request, $object_type ) {
// ...
}
and for the requested category object, we can get the term id with:
$term_id = $object['id'];