Skip Navigation

[Resolved] Get parent from relationship with PHP: toolset_get_related_posts

This support ticket is created 2 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by KonstantinS88 2 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#2220543

Unfortunately, neither the docs not the forum could help me. I want to find the post ID of a one to many relationship with PHP (i think i need the parent). How can i get this?

- The ID if the child is known
- The slug of the relationship is profil-nfc

Any help?

#2221123

Hi,

Thank you for contacting us and I'd be happy to assist.

To get the related posts in PHP code, you can use the "toolset_get_related_posts" function:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

For example, suppose you have a one-to-many relationship between "Shops" and "Books" with relationship slug "shops-books" and the current "Books" post ID is available in the $post->ID. Then the code to get the related parent "Shop" would look like this:


        // get children of a grandparent
        $query_by_element = $post->ID; // ID of post to get relationship from
        $relationship = 'shops-books'; // relationship slug
        $query_by_role_name = 'child'; // $query_by_element is a parent in this relation 
        $limit = 1000; // defaults
        $offset = 0; // defaults
        $args = array(); //nothing needed
        $return = 'post_id'; // We want Post ID
        $role_name_to_return = 'parent'; // We want children.
   
        $get_results = toolset_get_related_posts(
                        $query_by_element,
                        $relationship,
                        $query_by_role_name,
                        $limit,
                        $offset,
                        $args,
                        $return,
                        $role_name_to_return
                        );

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2222141

Unfortunately, this does not work and returns an empty array all the time. Also, I guess post parent and child were misplaced in the snippet. But with the linked docs, I got it working with just one line of code:

//retrieve parent post ID from a one to many relationsship when knowing the child post-ID
$parent_id = toolset_get_related_post( $post->ID, 'profil-nfc', 'parent' );
#2222145

My issue is resolved now. Thank you!

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