Skip Navigation

[Resolved] Conditional Statements for Parent/Child fields

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

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Konstantinos Galanakis 7 years, 6 months ago.

Assisted by: Konstantinos Galanakis.

Author
Posts
#444616

I am trying to: Display a field from the post parent if the post itself does have a value.

I have a custom field for "phone" to show a phone number and I need some help on how to put this conditional statement together.

I want to see if the "phone" field is not empty on the post and if it has a value to output it. But if it is empty then I want to see if the post parent has a value in a field of the same name (phone) and then display that.

My post parent is "clinics"

Thanks

#445098

Hello Farrel.

Thank you for contacting the Toolset Support.

In order to display a value of a custom field of the parent post to the View or Content Tempalte of the child post, what you need to do is use a code like below:

[types field="your-field-name" id="$parent-post-type-slug"][/types]

So for your case, you could do something like

Department Name: [wpv-post-title]<br />
Phone number: 
[wpv-conditional if="( $(wpcf-phone-number) ne '' )"]
[types field='phone-number'][/types]
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-phone-number) eq '' )"]
[types field="phone-number" id="$clinic"][/types]
[/wpv-conditional]

Where:

“clinic” is the custom post type slug for the parent post
“phone-number” is the custom post field slug regarding the phone number for both “Clinics” and “Departments”
“Departments” is the child custom post type that has “Clinics” as parent custom post type

In case you also want to check if the “phone-number” field value exists in the parent by using the code below

[wpv-conditional if="( '[types field="phone-number" id="$clinic"][/types]' ne '' )"]
Things to do if the "phone-number" field value exists in the parent ("Clinics")
[/wpv-conditional]

If you have any further issues, please let me know.

Regards

#445929

Great, thanks! I just have a question. I assume 'ne' means not empty. Is that correct?

And what does eq ' ' mean? Retrieve everything if you don't specify a value to be equal to?

#446008

Hello Farrel,

'ne' means "not equal".

In your case " eq '' " means "if the custom post field 'wpcf-phone-number' is not set or equals with an empty string'.

If you have any further issues, please let me know.

Regards

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