Skip Navigation

[Resolved] rest api toolset_get_related_post example

This support ticket is created 3 years, 4 months 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by cordalissC 3 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2263237

Tell us what you are trying to do?
I would like to add all related parents posts informations to a response of REST API endpoint.
If I can have all the ids, that will be create, if I can have all the meta fields, that will be better.

Is there any documentation that you are following?
this solution work only for single post_id https://toolset.com/forums/topic/relationship-in-api-rest-2/
I can't figure out, how to use "toolset_get_related_posts" instead of "toolset_get_related_post".

Thank you

#2263315

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I would like to know what relationship you are having - one -2-one or one-2-many?

Also, as I understand you want to get parent post based on the child post ID - is that correct? if yes, so in one-2-many post relationship when you pass child post ID it will have only one parent so in that case you should use toolset_get_related_post()

Can you please confirm what is your use case?

#2263403

Hello, thank you for your quick response.

I have two custom posts, "compagnies" and "membres" and a relationship many-to-many "compagnie-membre".

So one child "membres" can I have many parents "compagnies" and one parent "compagnies" can I have many child "membres".
I presume it's a one-2-many., if I understand well.

The goal it's to get all parents "compagnies" from one child post_id "membres", by using REST API GET.

#2263439

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you try to use the following code:

function get_parent_page_for_api_promociones( $object ) {
    //get the id of the post object array
    $post_id = $object['id'];

$result = toolset_get_related_posts(
  $post_id, // get posts related to the current post (product)
  'compagnie-membre', // relationship between the posts
  'child', // get posts where product is the child in given relationship
  999, 0, // pagination
  array(), // no extra args needed here
  'post_object', // return post objects
  'parent' // return the related parent post(s) in this relationship
);

    return $result;
}

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

#2263581

Thank you for your answer.

Almost done. How can I add taxonomies "compagnie-categorie-tax" and custom toolset-meta from parent.

This is my code:

add_filter( 'toolset_rest_run_exposure_filters', '__return_true' );

add_action( 'rest_api_init', 'create_api_posts_meta_field_compagnie_membre');

function create_api_posts_meta_field_compagnie_membre() {
register_rest_field( 'membres', 'compagnie_membre', array(
'get_callback' => 'get_parent_page_for_api_compagnie_membre',
'schema' => null,
)
);
}

function get_parent_page_for_api_compagnie_membre ( $object ) {
//get the id of the post object array
$post_id = $object['id'];

$result = toolset_get_related_posts(
$post_id, // get posts related to the current post (product)
'compagnie-membre', // relationship between the posts
'child', // get posts where product is the child in given relationship
999, 0, // pagination
array(), // no extra args needed here
'post_object', // return post objects
'parent' // return the related parent post(s) in this relationship
);

return $result;
}

#2263583

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

This is custom code and its beyond the scope of our support policy.

However, to get the taxonomy terms and custom fields attach to your parent, you should loop through the found parents one by one and use wp_get_object_terms() function to get related post terms and use get_post_meta() function to get related post custom field values and build array that holds basic parent post information with taxonomy terms and custom field values and return that array.

#2267889

Thank you