Skip Navigation

[Resolved] Check if a parent exists, or parent fields exist

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

Problem:
How can I check in a View, if the posts in the Loop have parents, or specific parent fields exist?

Solution:
You can check if Parent Fields exist, or you could check if the Hidden "Parent ID" Field of your Post holds an ID (checks if parent exists)

For this to work, you must first enable the Hidden Field

_wpcf_bleongs_parent-post-type-slug_id

in Toolset > Settings > Front End Content > Hidden custom fields

Then, you can construct your Conditional HTML with the GUI "Conditional Display" in your View.
This would look similar to this code:

[wpv-conditional if="( '[wpv-post-field name='_wpcf_bleongs_parent-post-type-slug_id']' ne '' )"]SOMETHING[/wpv-conditional]

If you want to check single Fields only, you can do the same, but of course, you would choose to compare another particular Field.

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-types-fields-and-custom-fields/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/
https://toolset.com/forums/topic/conditional-output-with-post-relations/#post-402476

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.

This topic contains 2 replies, has 2 voices.

Last updated by Mr. DJ 5 years, 3 months ago.

Assigned support staff: Beda.

Author
Posts
#402317

Let my try to explain what I want to achieve with this example:

I have a CPT called BRAND and a CPT called MODEL
MODEL is a child of BRAND.

If there is a relationship between BRAND and MODEL, I want to display fields from BRAND in my MODEL-view.
No problem, I use: <h3>This [wpv-post-title] is made by brand: [wpv-post-title id="$brand"]</h3> etc.

If there is NO relationship between the BRAND and MODEL, I want to display another text: <h3>This [wpv-post-title] has no brand</h3>.

I've tried to achieve this with [wpv-conditional] but I got stuck. How can I check if there is no relationship and display different text?

Thanks!

#402476
Load Post Fields.png
Insert the hidden Field.png
Highlight the Data Source.png
Create the Conditional.png

You can check if Parent Fields exist, or even, you could check if the Hidden "Parent ID" Field of your Model holds an ID.

If not, of course, no relation exists, and if yes, it does, and then you can display all content you want.

I think the second is the most "efficient" variant as it avoids to check on every single Field of a parent.

For this to work, you must first enable the Hidden Field _wpcf_bleongs_parent-post-type-slug_id in Toolset > Settings > Front End Content > Hidden custom fields

Then, you can construct your Conditional HTML with the GUI "Conditional Display" in your View.
This would look similar to this code:

[wpv-conditional if="( '[wpv-post-field name='_wpcf_bleongs_parent-post-type-slug_id']' ne '' )"]SOMETHING[/wpv-conditional]

I attached some screenshots to this process:
1. Activate the Hidden Field
2. Head to Views and add a conditional with the GUI, choose the Views ShortCode [wpv-post-field] as Data source and compare to "not empty."
3. Highlight the [wpv-post-field] and choose "Fields and Views" > "Post Fields" (expand them)
4. Find the hidden Field _wpcf_bleongs_parent-post-type-slug_id and insert it.

Above will check if a parent exists at all.

If you want to check single Fields only, you can do the same, but of course, you would choose to compare another particular Field.
This is elaborated here:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-types-fields-and-custom-fields/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/

#404261

Thanks, that works!
This opens up new possibilities for other projects.