Skip Navigation

[Resolved] Check if relationship exist

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

Last updated by camila 3 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#2201927

Hi,
I have a CPT name Property, that has one to too many relationships to Products (from Wocommerce). I need to check via PHP if a relationship exists on the Property, so I am using:
$relationship = toolset_get_relationship('title-search');
I get as an output an array, and I need to check if a relationship is set on the post. How can I do this using php?
thanks

#2202319

Hi,

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

The "toolset_get_relationship" function returns information of a post-relationship and not the relationship connections of a post.
( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_relationship )

To get the related post connections for a post, 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 "post-page", where "post" is parent and "page" is a child. To check if a "post" has any related "pages", the code would look like this:


	// check related posts
	$query_by_element = $post_id; // ID of post to get relationship connections from
	$relationship = 'post-page'; // relationship slug
	$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation 
	$limit = 1; // limit set to one as we just need to check if related post exists or not
	$offset = 0; // defaults
	$args = array(); //nothing needed
	$return = 'post_id'; // We want Post ID
	$role_name_to_return = 'child'; // 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
					);

	if(!empty($get_results)) {
		return 'Related result found';
	}
	else
	{
		return 'No related result found';
	}

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

regards,
Waqar

#2202815

My issue is resolved now. Thank you!

#2202839

Hi,
I just realize that I need to know the id of the child-related posts? How can I accomplish that?
thanks

#2202841

Hi.
I need to know the id of the child-related posts? How can I accomplish that?
thanks

#2202935

Thanks for writing back.

The IDs of the related posts, will be available in the array "$get_results".

Note: Remember to change "$limit = 1;" to a higher value like "$limit = 9999;", so that all the related posts can be queried.

For more personalized assistance around custom code, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2202959

Thanks for the answer! so would that code be $get_results[chid_post] ?? I am just not sure how to access it? it's not that clear in the documentation, can you please help? thanks

#2205951

This is more a "PHP programming" question and not something related to Toolset plugins, specifically.

You can get the related post IDs from the array like this: $get_results[0], $get_results[1], ........

To loop through all the available related posts, you can use the 'foreach' loop, like this:


if(!empty($get_results)) {
	foreach ($get_results as $get_result ) {
		// the ID of related posts ID is available in $get_result
	}
}

Here are some useful guides on the topic of getting values from a PHP array:

hidden link
hidden link
hidden link

#2206037

Thank you!