Skip Navigation

[Resolved] Conditional output of parent post on child’s content template

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

Problem: I would like to use a conditional to test the value of a custom field on a related (parent) post. The conditional generated by the GUI does not seem to work.

Solution: There seems to be a problem with related post field conditionals inserted by the GUI. As a temporary workaround, use the wpv-post-field syntax instead of the $(field) syntax, like this:

[wpv-conditional if="( '[wpv-post-field name='wpcf-calendar-item-event-url' id='@calendar-item-event-date.parent']' ne '' )" debug="true"] <div class="h3-s">Website</div>
  <div class="h4 info website">[types field='calendar-item-event-url' item='@calendar-item-event-date.parent' target='_blank'][/types]</div>
</div>[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 3 voices.

Last updated by Christian Cox 5 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1072636

I am trying to:
We have a parent content type, calendar-item, that has children, event-date.
On the content template for event-date, we'd like to show a value from the parent, calendar-item-event-url, including some wrapping html, only if the parent contains a value for calendar-item-event-url
We're using this code:

[wpv-conditional if="( $(wpcf-calendar-item-event-url).id(calendar-item) ne '' )"]<div class="h3-s">Website</div>
  <div class="h4 info website">[types field='calendar-item-event-url' item='@calendar-item-event-date.parent' target='_blank'][/types]</div>[/wpv-conditional]

Link to a page where the issue can be seen:
hidden link
I expected to see:
A website URL under the field that shows LOCATION
BENTONVILLE, AR, USA

Instead, I got:
Nothing in that section

#1073385
Bildschirmfoto 2018-08-04 um 13.51.05.png

Strange, I can see it.

I see your condition is:

[wpv-conditional if="( $(wpcf-calendar-item-event-url).id(calendar-item) ne '' )"]
  <div class="h3-s">Website</div>
  <div class="h4 info website">[types field='calendar-item-event-url' item='@calendar-item-event-date.parent' target='_blank'][/types]</div>[/wpv-conditional]

I can see that exact <div class="h3-s">Website</div> on the URL you shared (see screenshot).
I also see the URL.

The condition you apply above, if you think it does not work, I suggest to debug it like this:

1. Change the code, and only output this:

START
[types field='calendar-item-event-url' item='@calendar-item-event-date.parent' target='_blank'][/types]
END

What do you see?
You should see:
START hidden link link) END

If so, then follow the next step.
If not, the Field is not existent, has no value, or The ShortCode was not inserted with the GUI and is wrong.

2. Change the code to:
[wpv-conditional if="( '1' eq '1' )"]
<div class="h3-s">Website</div>
<div class="h4 info website">[types field='calendar-item-event-url' item='@calendar-item-event-date.parent' target='_blank'][/types]</div>[/wpv-conditional]

Do you still see the correct output?

If not, there is a conflict with another Plugin or theme, as that condition does work out of the box in any case (it will always show the stuff inside the condition)

If you see what is expected, you an go ahead and start to change the condition step by step t a more complex one.

Be sure that the field you check upon (wpcf-calendar-item-event-url).id(calendar-item) ) really exists, and is correctly populated.
Try to output it with the ShortCode of Toolset, to see what it actually outputs.
Then, compare to the exact output values.

If all this does not work, would you mind to let me know:
- where you edit this precise condition
- where you edit the fields involved
- other significant details you may consider important
==> Please make sure no plugin is outdated and there is not interference with them either.

I would then log in and check what is happening, but I will not change the things.

#1073575

I checked:

1) It outputs the URL correctly

2) The 1=1 conditional outputs the URL correctly

3) I'm editing here: hidden link
And checking output here: hidden link

#1073576

Feel free to test in that content template--it is not in production.

#1074375

And checking output here: hidden link;
Careful - this URL is not on HTTPS, and it has a cached, outdated version of the page that is not affected by updates to this Content Template. Check the page on HTTPS to see the updates.

I was able to get this alternative version of the conditional working as expected:

[wpv-conditional if="( '[wpv-post-field name='wpcf-calendar-item-event-url' id='@calendar-item-event-date.parent']' ne '' )" debug="true"] <div class="h3-s">Website</div>
  <div class="h4 info website">[types field='calendar-item-event-url' item='@calendar-item-event-date.parent' target='_blank'][/types]</div>
</div>[/wpv-conditional]

However, I'm not sure why the conditional generated by the GUI is not working correctly. I'm asking my 2nd tier support team to take a closer look. In the meantime, check the results of the change I made and confirm the conditional is working as expected. Then you can use this conditional template to test other parent fields if necessary.

#1076593

Looks like that worked! Thanks.
I tried it with "cost" as well, and it didn't work when there was a dollar sign in the value of the field, but it did work without the dollar sign--that was odd.

#1192049

Just a quick update to say that Views 2.7.3 will include a fix for the problem where the $ syntax isn't working with parent fields. Once this version is released, the workaround will no longer be required.

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