Skip Navigation

[Resolved] expose related posts in REST API

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

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:

  • Parent slug: "parent-cpt".
  • Child slug: "child-cpt".
  • Relationship slug:
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 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: Africa/Casablanca (GMT+01:00)

This topic contains 5 replies, has 2 voices.

Last updated by avansisI-2 4 years ago.

Assisted by: Jamal.

Author
Posts
#1858713

I have a issue with this code

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

And parent-page is 0
Could you help me?

#1859035
#1859075

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 hidden link

I used the following and slugs:
- Parent slug: "parent-cpt".
- Child slug: "child-cpt".
- Relationship slug:

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,
    )
  );
}

I hope this helps. Let me know if you have any questions.

#1859499

Hi Jamal,

Thanks for you reply. With this code doesnt appear parent_page in api

you can see it here
/wp-json/wp/v2/bonos

#1859779

I created this code but it keeps sending me a 0 in

"campana_bonos": 0,

//AÑADIR RELATIONSHIPS BONOS
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');
}
 
add_action( 'rest_api_init', 'create_api_posts_meta_field_bonos');
function create_api_posts_meta_field_bonos() {
  register_rest_field( 
    'bonos', 
    'campana_bonos', 
    array(
      'get_callback' => 'get_parent_page_for_api_bonos',
      'schema' => null,
    )
  );
}
#1859781

There was an issue with your code in the toolset_get_related_post function. The second argument should be the slug of the relationship "campana-bonos-bono".
I fixed it in your code and it works as you may see in this screenshot hidden link

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, 'campana-bonos-bono');
}
#1859791

My issue is resolved now. Thank you!