Saltar navegación

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

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

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 hace 4 años, 6 meses. 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 -

Zona horaria del colaborador: Asia/Hong_Kong (GMT+08:00)

Etiquetado: 

Este tema contiene 4 respuestas, tiene 2 mensajes.

Última actualización por avansisI-2 hace 4 años, 6 meses.

Asistido por: Luo Yang.

Autor
Mensajes
#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!