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
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
My issue is resolved now. Thank you!
Hi,
I just realize that I need to know the id of the child-related posts? How can I accomplish that?
thanks
Hi.
I need to know the id of the child-related posts? How can I accomplish that?
thanks
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/
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
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