Skip Navigation

[Resolved] get parent id using intermediary id in php

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

Problem: I would like to be able to get the ids of both related posts in a M2M relationship using the PHP Post Relationships API, when the intermediary post ID is known.

Solution: Use the toolset_get_related_posts API to retrieve related post information using the intermediary ID like this:

$book = toolset_get_related_posts(
    123,
    'book-library',
    'intermediary',
    1000000,0,
    array(),
    'post_id',
    'parent'
  );
// returns Array( [0] => 234 )
 
$library = toolset_get_related_posts(
    123,
    'book-library',
    'intermediary',
    1000000,0,
    array(),
    'post_id',
    'child'
  );
// returns Array( [0] => 345 )

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

This support ticket is created 6 years, 3 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by takoL 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#956534

Tell us what you are trying to do?

I am trying to get the parent ID's from an intermediary post using php.
Is it possible to get the parent ids when I have the post ID of an intermediary post using php?

Is there any documentation that you are following?

I have tried several variations of the toolset_get_parent_post_by_type(), toolset_get_related_post() and toolset_get_related_posts() methods but so far no luck.

Maybe I am having a heat stroke and not thinking clearly but I can't seem to find a way.
Maybe you can help.

Thanks in advance.

Cheers,

Tako

#956692

Hi, here are two examples where "book" and "library" are the parent and child post types, respectively, the relationship slug is "book-library", and the intermediary post ID is 123:

$book = toolset_get_related_posts(
    123,
    'book-library',
    'intermediary',
    1000000,0,
    array(),
    'post_id',
    'parent'
  );
// returns Array( [0] => 234 )

$library = toolset_get_related_posts(
    123,
    'book-library',
    'intermediary',
    1000000,0,
    array(),
    'post_id',
    'child'
  );
// returns Array( [0] => 345 )

Assuming the 123 is correct, you should get an array of 1 post ID returned. Show me your code if you can't get it to work, I'll take a look.

#957178

Hi Christian,

Thanks for the reply, your code works perfectly. I guess I was a bit confused (I will blame it on a sunstroke) about what relationship to use when starting from the intermediary.

Cheers and thanks again,

Tako