Skip Navigation

[Resolved] Conditional display if parent is a certain post type

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

Problem:

Use custom field value in shortcode [wpv-conditional].

Solution:

The client has found the problem:

the pdf-file is not in the parent but in the current CPT

https://toolset.com/forums/topic/conditional-display-if-parent-is-a-certain-post-type/#post-898510

Relevant Documentation:

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

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 11 replies, has 4 voices.

Last updated by Louis-Philippe 5 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#858012

***Tell us what you are trying to do?***
I am building a list view where i query "registration" post type and I display different fields.
Registration is a child of either workshop, formation or travel. Certain fields need to be picked up in the parent.
I was able to retrieve the featured image of the parents like this
[code]
<!-- /*FORMATIONS*/ -->
[wpv-conditional if="( '[wpv-post-id id="$formation"]' )"]
<a href="[wpv-post-url id='$formation']">
[wpv-post-featured-image id="$formation"]
</a>
[/wpv-conditional]
[/code]

Then when trying to link the post_url the same condition won't work for all my registration.
I try many different conditional statement that I found here on the forum but I can't quite get it
such as
[code]
[wpv-conditional if="( $(wpcf-description).id(workshop) ne '' )"]
<a style="color:#000:" href="[wpv-post-url id="$workshop"]">
[types field='titre-court-de-l-evenement' output='raw'][/types]
</a>
[/wpv-conditional]
[/code]
or
[code]
[wpv-conditional if="( $(_wpcf_belongs_workshop) ne '' ) )"]
Display workshop title and link
[/wpv-conditional]
[/code]

Please help me...

What is the link to your site?
hidden link

#858840

Hi, assuming you're not using the new M2M beta plugins, this syntax should work:

[wpv-conditional if="('[wpv-post-field name='wpcf-description' id='$workshop']' ne '' )"]
**ADD SOME PLAIN TEXT HERE SO SOMETHING IS VISIBLE EVEN IF THE TITRE CUSTOM FIELD IS EMPTY**
<a style="color:#000:" href="[wpv-post-url id="$workshop"]">
[types field='titre-court-de-l-evenement' output='raw'][/types]
</a>
[/wpv-conditional]

If this is not working as expected, please copy + paste the wpv-post-field shortcode outside the conditional, and add the debug parameter to the conditional shortcode:

Workshop description: [wpv-post-field name='wpcf-description' id='$workshop']<br />
[wpv-conditional if="('[wpv-post-field name='wpcf-description' id='$workshop']' ne '' )" debug="true"]

Let me know what you find out.

#859335

That is really strange. In pink is the output of the workshop title without any condition and all worked fine!
There is just two of my workshops that won't display. But after observation, I can say that those 2 activities contain apostrophe in the name like --> l'automne and when i test with other title with apostrophes it's not working.

Is there a runaround without changing my workshop names (apostrophes are important in French) ?

Thank you
ps. your code worked super fine!

#860311

I don't think there should be any problems with apostrophe characters in post titles unless you try to use the post title in a conditional. I would recommend using post slug in conditionals instead. Can I log in to your wp-admin area to see what's going on here? Private reply fields are enabled here.

#861767

Also, in frontend there is 3 event without the link in the title ( 2 cloud in shape of horse and the one below) and I use the field custom field "titre" that contains the apostrophe.
Item #1 is this Inscription post: hidden link
The parent Formation post: hidden link
Here is the conditional:

[wpv-conditional if="('[wpv-post-field name='wpcf-titre' id='$formation']' ne '' )"]
*F*
<a style="color:#000:" href="[wpv-post-url id="$formation"]">
[types field='titre-court-de-l-evenement' output='raw'][/types]
</a>
[/wpv-conditional] 

The problem here is that there is no custom field 'wpcf-titre' on the Formation post type. Are you comparing the field 'wpcf-titre-court-de-l-evenement', or should it be [wpv-post-title id='$formation']?

Item #2 and Item #3:
I think you're right, the problem is the apostrophe. We have a known issue related to this problem: https://toolset.com/errata/conditionals-including-special-characters-might-fail/
I was able to change the titre field to use an encoded apostrophe character:

&apos;

...and now the View seems to be working as expected. Can you confirm? If this works, you should be able to change any of the apostrophe characters to their HTML equivalent.

#876671

Thank you.
The strangest thing is that I have another custom field that I try to build the same way and it's not working neither.
It's a pdf that i need to display depending on the cpt it belongs to.

If cpt-x display button with link to the pdf in parent.
When i built the condition, it shows a button for every cpt or a link to the current page.
Sorry I took long to come back
And thank you for your precious help.
I really try to understand this conditional to parent and its behaviour!

Lp (*

#880764

Can you copy + paste the conditional here for me to review?

#883412

Yes, thank you
I try those 3
1-

[wpv-conditional if="('[wpv-post-field name='lien-vers-le-pdf' id='$atelier-et-rite']' ne '' )"]
<span class="btn-container">
      <a href="[types field='lien-vers-le-pdf' link='true' output='raw' id='$atelier-et-rite'][/types]" class="custom-link btn border-width-0 btn-color-204472 btn-square btn-outline btn-icon-right" target="_blank">*A* PDF <i class="fa fa-chevron-right"></i></a>
       </span>
[/wpv-conditional]

2-

[wpv-conditional if="( '[wpv-post-id id='$atelier-et-rite']' ne '' )"]
<span class="btn-container">
      <a href="[types field='lien-vers-le-pdf' link='true' output='raw' id='$atelier-et-rite'][/types]" class="custom-link btn border-width-0 btn-color-204472 btn-square btn-outline btn-icon-right" target="_blank">*A* PDF <i class="fa fa-chevron-right"></i></a>
       </span>
[/wpv-conditional]
 

3-

[wpv-conditional if="( $(wpcf-lien-vers-le-pdf).id($atelier-et-rite) )"]  
	<span class="btn-container">
      <a href="[types field='lien-vers-le-pdf' link='true' output='raw' id='$atelier-et-rite'][/types]" class="custom-link btn border-width-0 btn-color-204472 btn-square btn-outline btn-icon-right" target="_blank">*A* PDF <i class="fa fa-chevron-right"></i></a>
       </span>
[/wpv-conditional]

Th

#888359

Nigel
Supporter

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

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

Hi there

Christian is on vacation this week, so I'll will carry on here if that's okay.

Looking at your last update, the third format would only be relevant if you are using the current beta versions.

Assuming you are using the stable plugin versions then the $ format to specify the parent should be correct.

Now, I understand that the context for where you are using these conditional shortcodes is when displaying a registration custom post, and you want to conditionally display a button that links to a "lien-vers-le-pdf" custom field that belongs to a "atelier-et-rite" parent post. (The button should only display if such a custom field exists.)

Is that correct?

#889352

Now, I understand that the context for where you are using these conditional shortcodes is when displaying a registration custom post, and you want to conditionally display a button that links to a "lien-vers-le-pdf" custom field that belongs to a "atelier-et-rite" parent post. (The button should only display if such a custom field exists.)

Yes that is the case.

I have 3 different (formation, voyage, atelier-et-rite) CPT that share one child (inscription) and I want to get the link from the custom field 'lien-vers-le-pdf' that belong to the right parent.

If parent 'atelier-et-rite' has non empty 'lien-vers-le-pdf' display url of 'lien-vers-le-pdf' as a button link.
if parent is not 'atelier-et-rite' or has empty 'lien-vers-le-pdf' display nothing.

Thank you for your help !

#891517

Hello,

I will take care of this thread, I suggest you try to debug the codes manually, for example:

You can output the parent field value directly:
[wpv-post-field name='lien-vers-le-pdf' id='$atelier-et-rite']
Check if it does output field value correctly.

If the custom field "lien-vers-le-pdf" is created with Types plugin, you will need pre-pend "wpcf-" before the field slug in Views shortcode [wpv-post-field], for example:
[wpv-post-field name='wpcf-lien-vers-le-pdf' id='$atelier-et-rite']

Then use it in the wpv-conditional shortcode

#898510

HHHH- I'm really sorry, the pdf-file is not in the parent but in the current CPT- how stupid I feel just now, really.

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