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');
}
Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://developer.wordpress.org/reference/functions/register_rest_field/
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
| Sun | Mon | Tue | Wed | Thu | Fri | Sat | 
|---|---|---|---|---|---|---|
| 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - | 
| 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - | 
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 21 replies, has 2 voices.
Last updated by 4 years, 11 months ago.
Assisted by: Christian Cox.