I'm still struggling to find a way to accomplish something that I so far have not fully accomplished, yet it would seem easier to do than my experience so far.
I have two custom post-types: Animal and Vegetable. Animal has two fields: Field1 and Field2; Vegetable has three fields: FieldA, FieldB, FieldC. What I want to do is create a view that does the following:
For each Animal post, compare the value in Animals Field1 with the values in Vegetable FieldA and if there is a match, display Animal Field2 along with FieldB and FieldC for each post from Vegetable in which there is a match. Finally ,if there is not a match, display neither Animal nor Vegetable; in other words, display nothing. So the result would look something like this:
<u>Cow</u>
*Grass - Green
*Carrot - Orange
*Potato - Yellow
<u>Pig</u>
*Cucumber - Green
<u>Horse</u>
*Tomato - Red
*Lettuce - Green
Is this possible? I'm willing at this point to figure out some custom coding, if I can get some guidance on how to apply it to this particular example.
Hello,
If I am right, in your case:
one "Animal" post can connect to multiple "Vegetable" posts
one "Vegetable" post can connect to multiple "Animal" posts,
So you can setup many-to-many relationship between post type Animal and Vegetable
https://toolset.com/documentation/post-relationships/
But the post type relationship is not required in this case, you just need to use custom field to link Animal and Vegetable posts.
For example, In the single Animal post,
1) you can get the Animals Field1 value with Types/Views shortcode, use it as shortcode attribute "animals-field1" of below view.
https://toolset.com/documentation/user-guides/passing-arguments-to-views/#controlling-the-filter-with-shortcode-attributes
2) Create a post view, list "Vegetable" posts, filter by
"Vegetable FieldA" field value is same as the shortcode attribute "animals-field1"
https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/
Then in view's loop section, you can display the result as what you want.
Thanks for your reply. Two follow-up questions:
1) On the post relationship issue, am I correct that establishing these relationships requires that I edit each record individually to establish the relationship? If so that's not practical in my instance because the records are created via an import. If there is some way to establish the relationship globally, please explain how that is done.
2) On your explanation about how to join the fields, I think I understand how to use that method (although it's pretty tedious). But, your answer does not address my related question about how to not display anything when there is no match. So assume that Field1 of my Animal post type is a heading and below that is listed the "vegetable" posts that match. I know that I can edit out the default "no posts found" so that nothing is shown where vegetable posts would be. But the field for Animal still displays. If there is no match, how do I set up the view so that the heading doesn't display either.(See screenshot for example).
Q1) establishing these relationships requires that I edit each record individually to establish the relationship?
Yes, you need to edit each record individually to establish the relationship.
As I mentioned above, the post type relationship is not required in your case.
Q2) If there is no match, how do I set up the view so that the heading doesn't display either.
I assume you are going to display nothing(including Animal post information) when there isn't any related "vegetable" posts.
You can move the Animal post information insider "Vegetable" post view, for example:
1) Pass the Animal post ID to the "Vegetable" post view in Views attribute, like this:
[wpv-view name="vegetable post" animalid="[wpv-post-id]"]
In the post view "vegetable post", you can get the attribute "animalid" value with shortcode [wpv-attribute name="animalid"]
Then use it to display the Animal post information, like this:
[wpv-post-title id='[wpv-attribute name="animalid"]']
More help:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-attribute
I think this is getting me closer, but I need more guidance on how to combine the two things you have described. I'm having trouble following how the snippets from the two different responses you have provided would be incorporated together. Can you give me an example code?
I have already provided the example shortcode in above answer:
https://toolset.com/forums/topic/post-relationships-7/#post-1069978
If you still need assistance for it, please provide a test site with the same problem, and fill below private message box with credentials, also point out the problem page URL and view URL, I need a live website to test and debug
Thanks for the details, I have done below modification in your website:
1) Edit the parent view "T_Fruits"
in section "Loop Editor":
a) Remove the codes for displaying current fruit post:
<li>[wpv-post-body view_template="Loop item in T_Fruits"]</li>
b) And pass fruit post ID to child view as attribute "fruit_id":
[wpv-view name="t_color-view" ... fruit_id="[wpv-post-id]"]
2) Edit the child view: T_Color View
within shortcode [wpv-items-found] ... [/wpv-items-found]
You can get the fruit post ID with shortcode: [wpv-attribute name='fruit_id']
and display the fruit post content tempalte with shortcode:
<li>[wpv-post-body view_template="Loop item in T_Fruits" id="[wpv-attribute name='fruit_id']</li>
You can test the result here:
hidden link
Check if it is what you want.
This seems to do what I want! I"m going to test it further but this looks great for now! Thanks.
Can you make the links to my test site private in the solution that you provided?
Yes, all your website URLs are removed.