I am using the WP REST API to return data from a custom post that has a custom taxonomy. This is my code -
add_action( 'rest_api_init', 'register_book_type' );
function register_book_type() {
register_rest_field( 'lesson',
'book_type',
array(
'get_callback' => 'get_book_type',
'update_callback' => null,
'schema' => null,
)
);
}
function get_book_type( $object, $field_name, $request ) {
$name = [];
$object = get_terms(array(
'taxonomy' => 'book_type'
));
foreach ($object as $term) :
array_push($name, $term->name.' ');
endforeach;
return implode('/ ', $name);
}
This returns all the taxonomy names, but I need it to return only the taxonomy names that apply to the specific post.
How can I change this so it only displays taxonomy names for the specific post?
CodePudding user response:
I changed $object = get_term to
$object = get_the_terms( $post->ID, 'book_type' );
and this pulls for the specific post