Problem:
The user would like to expose some related posts as a field in the REST API responses.
Solution:
I run a small test and I found out that the issue in your code is the dash in the REST field name "parent-page", the code works with an underscore "parent_page". Check this screenshot http://prntscr.com/vr8azl
I used the following and slugs:
function get_parent_page_for_api_bonos( $object ) { //get the id of the post object array $post_id = $object['id']; //return the post meta return toolset_get_related_post( $post_id, 'parent-cpt-child-cpt'); } add_action( 'rest_api_init', 'create_api_posts_meta_field_bonos'); function create_api_posts_meta_field_bonos() { register_rest_field( 'child-cpt', 'parent_page', array( 'get_callback' => 'get_parent_page_for_api_bonos', 'schema' => null, ) ); }
Relevant Documentation:
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 |
---|---|---|---|---|---|---|
9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | - | - | 9:00 – 13:00 |
14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - | - | 14:00 – 18:00 |
Supporter timezone: Africa/Casablanca (GMT+01:00)