Skip Navigation

[Resolved] Use conditional output to compare fields from different post types and display a

This support ticket is created 5 years, 10 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/Karachi (GMT+05:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by briana-5 5 years, 9 months ago.

Assisted by: Waqar.

Author
Posts
#1205027

I have two custom post types...let's call them Post-Type1 and Post-Type2. I've built a content template for PostType1. In that template, I'm trying to use the conditional display function to compare a field in PostType1 with a field in PostType2 and where the two field values match, display a different field from Post Type2.

So the expression looks like this

[wpv-conditional if="( $(wpcf-current-location-bills) eq '$(wpcf-code-locations)' )"][THE FIELD I WANT TO DISPLAY][/wpv-conditional]

Is this sort of comparison supported? I could not get it to display anything, even just placing placeholder text in place of the field, which suggests that a comparison between fields of different post types is not supported. But perhaps I am doing something wrong. Can you clarify whether this is possible using conditional output?

#1205314

Hi Briana,

Thank you for contacting us and I'll be happy to assist.

The comparison between fields of different post types is supported, but you'll need to specify the post ID of the different post ( Post-Type2 ), with which you'd like to perform the comparison.

You can use the "types" fields API shortcode, to get the custom field values from the current and the different post:
https://toolset.com/documentation/customizing-sites-using-php/functions/

To get the value of the custom field from the other than the current post, you'll need to specify its ID using the "Item" attribute:
https://toolset.com/documentation/user-guides/views-shortcodes/item-attribute/

For example, if the ID of the Post-Type2 that you'd like to use for the comparison is "100", you can use it in the conditional shortcode as:


[wpv-conditional if="( '[types field='current-location-bills'][/types]' eq '[types field='code-locations' item='100'][/types]' )" ]
[THE FIELD I WANT TO DISPLAY]
[/wpv-conditional]

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1205336

Thank you for your response. Not sure if this is a workable solution or not.

If i'm following your answer, to compare values between the current post and another post (the second post), I would need to specify the post ID for the second post. I can see how that would work if I just wanted to compare the current post with one other known post.

But what I'm trying to do is compare the current post of post type one with ALL posts in the second post type and then return a value from a post that matches.

For example: I have a post type called ID and another post type called Names. Each post in ID has a field called "Code" that contains a two digit code. Each Names post has a field called "code" that also contains a two digit code and a second field that has a name that corresponds to that code.

ID post type and Names Post type have 100s of posts each. What I want is for the current post in ID to look at each of the posts in Names and compare the code field from the current ID post type to each of the Names posts and where the "code" field In a Names post matches the value of the "code" field in the current ID post, return the value from the second field for the Names post that matches.

Is that possible?

#1205976

Hi Briana,

Thanks for writing back and for sharing further details.

What you're planning is possible, but instead of using "wpv-conditional" shortcode, it would be better to use a post view:
https://toolset.com/documentation/getting-started-with-toolset/create-and-display-custom-lists-of-content/

Through a post view, you can get a list of all "Names" posts, which have the same code value in their custom field, as the current ID post type. Once you'll have the required posts in the view's loop, you'll be able to show any fields from them.

You can pass the current ID post's code value, to filter the view through the view's shortcode attribute, as explained at this guide:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/#controlling-the-filter-with-shortcode-attributes

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1221347

My issue is resolved now. Thank you!