Skip Navigation

[Resolved] Post relationship query with toolset_get_related_posts()

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

Problem: I have a M2M relationship set up using an intermediary post type. I would like to use PHP and the post relationships API to query get all related parent posts of a known child post.

Solution: Use this code as a template:

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

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

This support ticket is created 5 years, 9 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 5 replies, has 2 voices.

Last updated by Christian Cox 5 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1226154

I'm trying to retrieve the posts connected via an intermediary post.
I did not succeed after trying with the documentation…

I have products (product) and artists (artiste) connected with an intermediary post type (artiste-product).

I want to retrieve via php the artists connected to the product I'm displaying.

$artists = toolset_get_related_posts(
get_the_ID(), // get posts related to this one
array( 'parent' => 'artiste' , 'intermediary' => 'artiste-product' ), // relationship between the posts
'parent', // get posts where $writer is the parent in given relationship
10, 1, // pagination
array(), // How was his surname, again…?
'post_object'
);

Could you help ?
Thanks.

#1226157

Hi, assuming product is the parent and artiste is the child in the relationship, your API call might look like this:

$artists = toolset_get_related_posts(
  get_the_ID(), // get posts related to this one
  'artiste-product', // relationship between the posts
  'parent', // get posts where product is the parent in given relationship
  10, 0, // pagination
  array(), // no extra args needed here
  'post_object', // return post objects
  'child' // return the child posts in this relationship
);

I can see you had 1 as the page offset, but I think 0 is probably what you need here unless you really want to skip 10 posts. If that's not working as expected, check get_the_ID() to confirm you're getting the Product ID as expected.

#1226173

Christian,

Thanks for the pagination glitch.

I still get an empty array and my get_the_ID() is working fine.
The relation is a many to many relationship with 'artiste-product' as the intermediary post-type.
Many the relationships are not well set in the query…

#1226813


The relation is a many to many relationship with 'artiste-product' as the intermediary post-type.

I think I had the parent and child reversed. Try this instead:

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

If that doesn't work, I'll need to log in and take a closer look.

#1226845

Yes it was this child/parent switch.
I will definitely need more documentation for me to understand this query.

Thank you!
You Rock.

#1226846

Okay glad that worked. I've asked our documentation team to enhance the information about this API and they're working on it. There are two formats for the data payload, but there's only one example and it's not comprehensive. In the meantime, feel free to reach out to support if you have questions.