I am having problems with the relationship in the API. as it returns "parent-page": 0,
This is my code. What is wrong?
//AÑADIR RELATIONSHIPS BONOS
add_action( 'rest_api_init', 'create_api_posts_meta_field_bonos');
function create_api_posts_meta_field_bonos() {
register_rest_field( 'bonos', 'parent-page', array(
'get_callback' => 'get_parent_page_for_api_bonos',
'schema' => null,
)
);
}
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, 'bonos');
}
Hello,
How do you setup the post type relationship "bonos"? is it relationship slug?
I suggest you follow our document to setup the PHP codes:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
$relationship - string|string[] Slug of the relationship to query by or an array with the parent and the child post type.
I've set it up just like this one does:
That's why I don't know what I'm doing wrong
//AÑADIR RELATIONSHIPS BONOS
add_action( 'rest_api_init', 'create_api_posts_meta_field_bonos');
function create_api_posts_meta_field_bonos() {
register_rest_field( 'bonos', 'parent-page', array(
'get_callback' => 'get_parent_page_for_api_bonos',
'schema' => null,
)
);
}
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, 'bonos');
}
Since it is a custom codes problem, please provide a test site with the same problem, also point out the problem REST API URL(where I can see the problem in front-end), where I can edit your custom PHP codes, I need to test and debug it in a live website. thanks
My issue is resolved now. Thank you!