Skip Navigation

[Resolved] Conditional Logic : Determine if post belongs to another post type

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

Problem:
Using conditional shortcodes, how could you test a child post to determine whether its parent post was of one post type or another?

Solution:
In the context of a child post you can refer to fields of the parent by adding the item attribute to shortcodes, e.g.

[wpv-post-title item="@relationship-slug.parent"]

So you can add a conditional shortcode that tests whether the parent title from a particular relationship is empty or not; that will tell you whether the parent is of a particular post type.

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/

This support ticket is created 6 years 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 3 replies, has 2 voices.

Last updated by nedG 6 years ago.

Assisted by: Nigel.

Author
Posts
#1168916

I have a Custom Post Type called ADDRESSES

I have 2 additional Custom Post Types called DISTRIBUTORS and INSTALLERS

An ADDRESS can belong to either a DISTRIBUTOR or an INSTALLER. (This allows me to be able to create only a single DISTRIBUTOR name and then assign many ADDRESSES to it as children. The same is true for INSTALLERS).

The problem is that I am creating a View, and I am querying the ADDRESSES in the View and I need to know if the current ADDRESS being queried in the View belongs to either a DISTRIBUTOR or an INSTALLER. I need some conditional logic to determine this and I cannot find out how to do it.

I have tried several methods. I know in the "old" relationship methods, we could use the wpcf_belongs_to_ID hidden custom field... but that doesn't appear to exist in this new methodology.

Am I missing something.

All I need is something like this....

[wpv-conditional if="( [wpcf_belongs_to_distribtor"] eq '1' )"] THIS IS A DISTRIBUTOR [/wpv-conditional]

How is this accomplished?

#1169053

I think this might be an answer but I was hoping there was a better way...
https://toolset.com/forums/topic/conditional-output-based-on-post-relationships/

#1169443

Nigel
Supporter

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

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

Hi Ned

There is a one-to-many relationship between distributors and addresses, so starting from an address, you would be able to output the post title of the parent distributor with...

[wpv-post-title item="@distributor-address.parent"]

...where distributor-address was the slug of the relationship.

If there were no parent distributor, because the address actually belongs to the similar installer-address relationship, then no post title would be output.

So that seems a reasonable test for your conditional shortcode, whether the title is empty or not.

#1169706

Ha! Actually I tried that exact same solution myself prior to submitting the ticket. I guess it was a matter of getting the "quotation marks" syntax correct in the conditional statement. I tried it numerous times... and eventually it worked. I just needed to stick with it I guess... instead of assuming that it wasn't working correctly.

Thanks Nigel.

The final syntax that worked (with the proper quotation marks) is..

[wpv-conditional if="( ('[wpv-post-title item='@distributor-address.parent']') ne '' )"]
DO THIS
[/wpv-conditional]