Skip Navigation

[Resolved] Conditional Output – how to determine whether child post has parent post

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to test whether a child post has a parent, using wpv-conditional.

Solution:
With the switch to Types 3 the format of the conditional statement required has changed, and using the GUI to insert fields will ensure the right format is used.

So, insert the field you want to test into the template using the Fields and Views button, using the post selection tab to identify what would be the parent.

Then manually copy and paste this into a wpv-conditional statement, to achieve something like this:

[wpv-conditional if="( '[wpv-post-id item='@relationship-slug.parent']' ne '' )"]
HAS PARENT
[/wpv-conditional]
This support ticket is created 6 years, 6 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by chuckH 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#918171

Hi. I have been banging my head on this for past couple of days...

I HAVE THE FOLLOWING DATA MODEL...
- parent post type called location
- child post type called session
- location has many sessions (one:many)
- view uses sessions for content selection

I WISH TO DO THE FOLLOWING...
- determine whether a session (child) post has corresponding location (parent) post

----------

1. WHAT I DID...
I tried the following expression to determine whether parent post exists by checking for presence of foreign key.

[wpv-conditional if="( $(_wpcf_belongs_location_id) ne '' )"]
Yes! child has parent post
[/wpv-conditional]

RESULT: Always evaluates FALSE

----------

2. WHAT I DID...
I then tried to determine whether a random field was present in parent post. If TRUE, then parent post must also exist.

[wpv-conditional if="( $(wpcf-location-name).id(location) ne '' )"]
Yes! child field (i.e. location-name) exists
[/wpv-conditional]

RESULT: Always evaluates FALSE

----------

I have read the documentation & support threads below:
https://toolset.com/forums/topic/conditional-if-a-parent-post-exists-and-its-custom-field-is-not-empty-then/

https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

Thank you for your help!

#918206

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Chuck

One quick note to mention you can add the debug="true" attribute to your wpv-conditional shortcode and then on the front-end it will show you what is being compared which can help track down problems.

In this particular case the right way to do this depends on whether you are using relationships newly created in Types 3, or relationships created in Types 2 that you have not migrated, or have migrated.

This works for newly created relationships and should work for migrated relationships:

[wpv-conditional if="( '[wpv-post-id item='@relationship-slug.parent']' ne '' )"]
HAS PARENT
[/wpv-conditional]

I generated that in two steps. I inserted the wpv-conditional with a test of the wpv-post-id shortcode.

The GUI doesn't let you add parameters, so I inserted the wpv-post-id shortcode directly, using the Post selection tab to specify that I wanted the source to be the relevant parent to get the correct format.

I then replaced the vanilla wpv-post-id in the conditional with this (adjusting quotes as required).

Do you want to try that?

#918475

Hi Nigel,

Thank you for your prompt assistance.

The solution (and debug tip) you provided worked great! In my case, the following conditional statement to evaluate whether parent exists worked:

[wpv-conditional if="( '[wpv-post-id item='@location-session.parent']' ne '' )" debug="true"]
HAS PARENT
[/wpv-conditional]