Skip Navigation

[Resolved] Get post parent with new Post Relationships API (php)

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

Problem: I would like to get the parent post Course of the current post Product using the new Post Relationships API, but it doesn't seem to be working. I have tried both of these syntaxes:

$course = toolset_get_parent_post_by_type( $product, 'course' );
$courses = get_posts( 
        array(
            'post_type' => 'course',
            'numberposts' => -1,
            'toolset_relationships' => array(
               'role' => 'parent',
               'related_to' => $product_id,
               'relationship' => array( 'course', 'product' )
            ),
        )
    );

Solution: In this case, the relationships were created in the new system, so toolset_get_parent_post_by_type is not appropriate. The toolset_get_parent_post_by_type API is intended for use with legacy relationships, or relationships that were migrated from the legacy system. When specifying the relationship to query, the array syntax is intended for legacy relationships as well. The best approach in this case is to use the toolset_get_related_posts API, and the relationship slug instead of the post type slug array:

$product = get_post(69);
$course = toolset_get_related_post( $product, 'your-relationship-slug', 'parent');

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

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

Last updated by Valeriia 5 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#907884

Hi,

I'm getting lost a bit in this new API...

I'm trying to get a parent of a WooCommerce product which is set to be a Course Post Type.

This did not work, it returns nothing:

 $course = toolset_get_parent_post_by_type( $product, 'course' );

This returns all the courses, not only the parent:

$courses = get_posts( 
 		array(
  			'post_type' => 'course',
  			'numberposts' => -1,
			'toolset_relationships' => array(
			   'role' => 'parent',
			   'related_to' => $product_id,
			   'relationship' => array( 'course', 'product' )
			),
		)
	);

How do I do it?

Thanks.

#908420

Hi, can you tell me more about your current setup?
- You must use suppress_filters if you call get_posts with relationship criteria. There's a note in the documentation here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/ The preferred method is the dedicated API toolset_get_related_posts. Was this get_posts call just a test?
- What is the post status of the parent Course? There is currently a known issue where the API does not retrieve parent post information appropriately when the parent post status is something other than "publish"
- Was this post relationship created in Types 3.0, or was it created before 3.0 and migrated into the new relationship system?
- In the first code example, can you print_r $product and confirm the product variable is set correctly?
- In the second code example, can you error_log $product_id and confirm the product ID variable is set correctly?

#908703
s2018-06-05 (1).png
s2018-06-05.png

Hi Christian,

"- You must use suppress_filters if you call get_posts with relationship criteria. There's a note in the documentation here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/ The preferred method is the dedicated API toolset_get_related_posts. Was this get_posts call just a test?"

I started with toolset_get_parent_post_by_type and only when it didn't work, I tried get_posts.

I didn't notice the note on suppress_filters before. Anyway, tried it now and instead of all courses I got none. So it didn't help.

I also tried toolset_get_related_post, which returned 0.

"- What is the post status of the parent Course? There is currently a known issue where the API does not retrieve parent post information appropriately when the parent post status is something other than "publish""

It's published.

"- Was this post relationship created in Types 3.0, or was it created before 3.0 and migrated into the new relationship system?"

I just started this development, I thought it would be a good exercise to get used to the new API. So everything is new.

"- In the first code example, can you print_r $product and confirm the product variable is set correctly?"

Yes. For testing purposes I even retrieve the specific product by its id.

	$course = toolset_get_parent_post_by_type(get_post(69), 'course' ); 
// or
	$course = toolset_get_related_post(69, array( 'product', 'course' ));

"- In the second code example, can you error_log $product_id and confirm the product ID variable is set correctly?"

Same, I'm calling it by its ID now and it does not work.

	$courses = get_posts( 
 		array(
  			'post_type' => 'course',
  			'numberposts' => -1,
			'toolset_relationships' => array(
			   'role' => 'parent',
			   'related_to' => 69,
			   'relationship' => array( 'product', 'course' )
			),
			'suppress_filters' => false
		)
	);

---

Btw, the relationship type is 1:1. I attached 2 screenshots showing that the relationship is set for both.

Thanks.

#909071
$course = toolset_get_parent_post_by_type(get_post(69), 'course' );

This API is intended for use with legacy relationships, or with legacy relationships that have been migrated to the new system. Since your relationship was created in the new system, this API is not appropriate.

$course = toolset_get_related_post(69, array( 'product', 'course' ));
...and...
'relationship' => array( 'product', 'course' )

Both of these examples you shared use the array syntax to specify a relationship, but the array syntax is not for use with relationships created in the new system. You should specify a relationship slug for relationships created in the new system.

Here's some revised code:

$product = get_post(69);
$course = toolset_get_related_post( $product, 'your-relationship-slug', 'parent');

Replace your-relationship-slug with your actual one-to-one relationship slug. You can find the relationship slug by editing the one-to-one relationship.

Please let me know if this does not resolve the problem completely for you.

#909086

It worked, thanks.

Is there an updated version of the documentation somewhere or it's coming? I have spent quite some time to find some information on this and didn't see your version anywhere.

Thanks.

#909374

Sure, here's an updated doc with more information about the new Post Relationships API:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

#911087

Thanks Christian.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.