Skip Navigation

[Resolved] Get ID of parent post in relationship

This support ticket is created 6 years, 5 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.

This topic contains 1 reply, has 1 voice.

Last updated by Alexanndre Levan 6 years, 5 months ago.

Author
Posts
#1080167

I have a one-to-many relationship setup between the two post types.

One: "Auction Page"
Many: Products

Let's say I have a single Auction Page which is linked with Product A, and Product B.

What PHP code should I be using in my template file to get the ID of the parent Auction Page if the code is being display on the Product pages?

My goal is to create a button on the product pages that links back to the parent Auction page. So I need the ID to pass to the permalink function.

I am using the latest version of toolset, but the examples in the documentation have not been helpful: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

#1080173

After playing around with the code I finally found a working solution. Here is the snippet I used.

$product_id = get_post( get_the_ID() ); // Get ID of current product
$auction_parent_page_id = toolset_get_related_post( // Get ID of parent auction page
    $product_id,
    'auction-page-product', //slug of relationship
    'parent'
);

Then I was able to pass $auction_parent_page_id to the get_the_permalink() function to output my link correctly.