Skip Navigation

[Resolved] Display field from one Post Type based on match to field in another post type

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

Problem: A post type called sweaters has a field called color_code that contains an alphanumeric code like "BL6". A second post type called colors contains an alphanumeric field color_code and a second field color_name with the value "blue". How can I say "when the sweater post type field 'color_code' value matches the color post type field value 'color_code', display the color post type field 'color_name'"?

Solution:
- Create a View of Colors, filtered by the custom field "color_code", where the value is supplied by a shortcode attribute "color_code"
- In the Loop Output of this View, insert a Types field shortcode that displays the value of the "color_name" custom field.
- Insert the View of Colors in the Content Template or Layout used for Sweaters. Insert a Types field shortcode to supply the value of the color_code shortcode attribute, like this:

[wpv-view name='your-view-slug' color_code='[types field="color_code" output="raw"]']

Relevant Documentation:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

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

Last updated by Christian Cox 6 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#630300

Is there a way to define a view using conditional output or some other function so that it matches a value in a field in one post type with a value in a field in a second post type and then displays a different field from that second post type.

For example: A post type called sweaters has a field called color_code that contains an alphanumeric code like "BL6". A second post type called colors contains an alphanumeric field color_code and a second field color_name with the value "blue". How can I say "when the sweater post type field 'color_code' value matches the color post type field value 'color_code', display the color post type field 'color_name'"?

I know that this function can be done with post relationships but that appears to require me to manually edit each post to establish that relationship. In this case, I am importing hundreds of records which are periodically updated so it would be impossible to then go in and manually match each BL6 post with the blue post. Is there another way?

#630420

Hi, one way to do this is to place a filtered View of Colors on the Sweaters custom post.
- Create a View of Colors, filtered by the custom field "color_code", where the value is supplied by a shortcode attribute "color_code"
- In the Loop Output of this View, insert a Types field shortcode that displays the value of the "color_name" custom field.
- Insert the View of Colors in the Content Template or Layout used for Sweaters. Insert a Types field shortcode to supply the value of the color_code shortcode attribute, like this:
[wpv-view name='your-view-slug' color_code='[types field="color_code" output="raw"]']

This technique is called passing arguments into a View, and we have some documentation about it here:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

Let me know if you have additional questions about this.