Skip Navigation

[Resolved] I am having problems with the relationship in the API

This thread is resolved. Here is a description of the problem and solution.

Problem:

I am having problems with the relationship in the API, how to get related parent post in PHP codes.

Solution:

I suggest you follow our document to setup the PHP codes.
Relevant Documentation:

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

This support ticket is created 4 years ago. There's a good chance that you are reading advice that it now obsolete.

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: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by avansisI-2 4 years ago.

Assisted by: Luo Yang.

Author
Posts
#1858091

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');
}
#1858257

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.

#1858711

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');
}
#1858717

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

#1860087

My issue is resolved now. Thank you!