Skip Navigation

[Resolved] Detect if there are any relationshipp in one to one relationship

This support ticket is created 3 years, 8 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 replies, has 2 voices.

Last updated by charlie 3 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#1981891

Tell us what you are trying to do?In general when I want to detect if there are related posts i use this function and then I registered a shortcode. If this shortcode is 1 then there are related posts. like in this formula

add_shortcode( 'parent-post_video_musicale', 'parent_post_video_musicale_func' );
function parent_post_video_musicale_func( $atts ) {

$parent_posts = toolset_get_related_posts( get_the_ID() , 'post_video_musicale', 'child' );

$res = 0;
if (!empty($parent_posts)) $res = 1;

return $res;

}
What if in case the relationship is one to one, and no child or parent relationship?

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site? hidden link

#1982573

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Charlie,

Thank you for getting in touch. Given that you are using the toolset_get_related_posts() function, it would imply that you wrote this code for Many to Many or One to Many relationships.

However for One to One you can use the toolset_get_related_post() function. Notice post is singular and not plural.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

Even if the relationship is One to One, there is a Parent and a child. The first post type that is selected will be considered as the Parent and the second post type will be the child.

Please let me know if this helps.
Thanks,
Shane

#1982659

I tried to use "get_related_post" but it doesn't work
with this function so I have an ID
$parent_posts = toolset_get_related_post( get_the_ID() , 'post_video_musicale', 'child' );
how can I say that id is not equal to 0 but !=empity so to make the shortcode
parent-post_video_musicale
equal to 1?

#1982741

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Charlie,

This actually works for me in a simple test of a one to one relationship that i've setup.

I've checked on my previous response and saw i made an error in the function name, it should be "toolset_get_related_post()".

Testing the function on my end produces the 1 on the frontend when the posts are connected in a relationship.

how can I say that id is not equal to 0 but !=empity so to make the shortcode
parent-post_video_musicale

Having the !empty() function alone will suffice in this case because if there are no relationship the toolset_get_related_post() will return nothing.

Thanks,
Shane

#1983601

So in this function:
if (!empty($parent_posts)) $res = 1;

return $res;

}
what do I have to put inside to make the shortcode parent-post_video_musicale = 1? Thanks

#1983699

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Charlie,

Here is the shortcode that you should be using.

add_shortcode( 'parent-post_video_musicale', 'parent_post_video_musicale_func' );
function parent_post_video_musicale_func( $atts ) {

$parent_posts = toolset_get_related_post( get_the_ID() , 'post_video_musicale', 'child' );

$res = 0;
if (!empty($parent_posts)) $res = 1;

return $res;

}

Notice i've change toolset_get_related_posts() to toolset_get_related_post()

Also when you add the shortcode [parent-post_video_musicale] to your parent posts it will check if that parent post has a child and return 1 if there is a child post. Remember that the parent post is the first CPT that was added in the post relationship.

However from what I see on this shortcode it will only work when added to the parent post type. If you want to modify this to be used on both Parent post and Child posts then you will need to allow the shortcode to accept parameters. Like below.

add_shortcode( 'parent-post_video_musicale', 'parent_post_video_musicale_func' );
function parent_post_video_musicale_func( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'relationship' => '',
		),
		$atts
	);

$parent_posts = toolset_get_related_post( get_the_ID() , 'post_video_musicale', $atts['relationship'] );

$res = 0;
if (!empty($parent_posts)) $res = 1;

return $res;



So when you're using the shortcode you will need to specify if you want to check for the Parent or Child. Example [parent-post_video_musicale relationship='child']

Please let me know if my response provided any clarity or if you're still unsure.

Thanks,
Shane

#1983767

My issue is resolved now. Thank you!