Problem: I would like to add related parent post information to the response of a REST API endpoint for the child post type.
Solution: Use the following custom code as a guide for adding related post information in a custom registered field in your REST API response for the child post type:
add_action( 'rest_api_init', 'create_api_posts_meta_field_promociones');
function create_api_posts_meta_field_promociones() {
register_rest_field( 'promociones', 'parent-page', array(
'get_callback' => 'get_parent_page_for_api_promociones',
'schema' => null,
)
);
}
function get_parent_page_for_api_promociones( $object ) {
//get the id of the post object array
$post_id = $object['id'];
//return the related comercio post ID
return toolset_get_related_post( $post_id, 'comercio_promocion', 'parent');
}