Skip Navigation

[Resolved] Displaying appropriate intermediary post fields with a many to many relationship

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

Problem: When displaying a View of related posts in an M2M relationship, I would like to display custom field values from the intermediary post type (relationship), but the Types field shortcodes do not appear to be working correctly.

Solution: Be sure the Types field shortcodes use the proper item attribute syntax @relationship-slug.intermediary.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/

This support ticket is created 4 years, 1 month 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)

Author
Posts
#1870375
tac - template-for-recipes - v2.png
tac - list-ingredients - v2.png

Tell us what you are trying to do?
Display recipe ingredients & their intermediary post data.
- I CAN display the Ingredients associated to the recipe
- I CAN display intermediary post data BUT it's NOT related to the recipe (post) as it's displaying the 1st entry

For example I have a recipe Carrot Cake this uses 300g of carrots I add it in and all appears to work fine. I then add my 2nd recipe involving carrots (i.e. Carrot Soup) which has only 150g of carrots HOWEVER it's always pulling through the 300g from the first entry.

Is there any documentation that you are following?
I've used toolset for years but had never needed to use the intermediary post data before. I referred to this post https://toolset.com/forums/topic/trying-to-understand-the-intermediary-relationship/ BUT it's not that I'm having issues displaying the intermediary post data is pulling the associated entry I'm having issues with

Is there a similar example that we can see?
Not that I'm aware of

What is the link to your site?
hidden link

I'm including my loops as I have them now, any advise would be appreciated.

#1870921

Hi, the Template shortcodes and the View configurations look good to me. One thing I noticed in your loop template is that your Types field shortcodes are not explicitly closed - that could be part of the issue here:

...
[wpv-conditional if="( '[types field='grams' item='@ingredient-recipe.association']' ne '' )"]<b>[types field="grams" item="@ingredient-recipe.association"]g</b>[/wpv-conditional]
...

I would start by adding explicit closing tags to each Types field shortcode:

...
[wpv-conditional if="( '[types field='grams' item='@ingredient-recipe.association'][/types]' ne '' )"]<b>[types field="grams" item="@ingredient-recipe.association"][/types]g</b>[/wpv-conditional]
...

If that doesn't solve the problem, the next step is to determine whether the problem is in the conditionals or in the View elsewhere. Copy your current loop content and paste it somewhere as a backup, then replace the loop contents with simple Types field shortcodes without conditionals. Is the content correct now?

#1871627
tac - Recipe Comparison.png

I added the closing tags in with the logic and this made no difference unfortunately. I then removed all conditional logic from the output but it's still pulling in the most recent entry (sorry realised I said 1st entry in my original post).

<li>
<b>[types field="grams" item="@ingredient-recipe.association"][/types]g</b>
<b>[types field="millilitres" item="@ingredient-recipe.association"][/types]ml</b>
<b>[types field="teaspoons" item="@ingredient-recipe.association"][/types] tsp</b>
<b>[types field="quantity" item="@ingredient-recipe.association"][/types]</b> x
[wpv-post-title]</li>

Any suggestions where to go next?

#1872129

Maybe I'm misunderstanding the screenshots, but it looks like the List Ingredients View is nested inside another view of Recipes. Am I seeing that correctly, or is that just an effect of combining multiple screenshots into one? If it's nested in a View of Recipes, then the Post Relationship filter in the List Ingredients View should be configured to "Select posts in a Ingredients in Recipe relationship that are related to the current post in the loop", whereas now you have it configured to "Select posts in a Ingredients in Recipe relationship that are related to the Post where this View is shown". So it could be a relationship filter context issue.

If the issue persists after changing that filter configuration, it may be most efficient for me to log into your wp-admin area and see what might be causing the problem. Please provide login credentials in the private reply fields and I'll take a closer look.

#1874609
Screen Shot 2020-12-13 at 8.39.30 AM.png

Okay I made a quick change to run some tests, and it seems to have something to do with using a Loop Template in the View vs. placing the contents of the loop template directly in the loop. See the attachment here. I moved the contents of the Loop Template directly into the loop, and commented out the template shortcode temporarily. This changes the results in each individual Recipe post, for example, here: hidden link

Can you take a quick look and see if the results are more accurate now? I'm not sure where to look to see the View of Recipes, so I'm not sure of the results there. If you can show me a URL I'll check it as well.

#1874667
Exercises Vs Ingredients.jpg

Hi Christian,

That's great and I think we're almost there now (you can use any recipe post to compare, but I can advise this is pulling the correct data in now), albeit I'm not 100% sure why adding it in the top box yields different results. Is this a bug or is my understanding a little off? So whilst Recipe's are working with this setup Exercises > Workouts (with a near identical setup/issue) hasn't been resolved when I replicated your fix above. Was there something else I might have missed?

Admittedly there is a little more going on with this layout but you will see by viewing hidden link that Back Squat is still showing "Sets: 6 Reps: 300" as opposed to the input value of "Sets: 6 Reps: 4". For comparative purposes this URL hidden link should have the Back Squat displaying "Sets: 4 Reps: 4" (and like the other is actually showing "Sets: 6 Reps: 300")

Look forward to hearing from you.

#1874765

...albeit I'm not 100% sure why adding it in the top box yields different results. Is this a bug or is my understanding a little off?
It shouldn't be necessary to use this workaround, something is not working as expected here but I'm not 100% sure yet. I wanted to confirm the results are as expected first, since this determines the next steps. I will continue to investigate and give you an update.

So whilst Recipe's are working with this setup Exercises > Workouts (with a near identical setup/issue) hasn't been resolved when I replicated your fix above. Was there something else I might have missed?
The main difference I saw here was in the syntax of the "item" attributes of each Types field shortcode. You can see that in your most recent comparison screenshot. In the Recipe case, the item attribute syntax used the .intermediary identifier, while the Workout case used .association. The .intermediary identifier is the correct syntax. The .intermediary syntax in the Recipes case was generated automatically when I inserted content using the GUI, but I'm not sure about the Workout case. I made that change manually in the List Exercises in a Workout View. Were the Types field shortcodes generated using some Toolset GUI, or hand-written?
If they were hand-written, the initial syntax was incorrect. If there is some documentation showing that .association is the correct identifier, I'd like to get that corrected. If the field shortcodes were generated automatically using some Toolset GUI, that could be a bug that produces incorrect markup. So I would need to know more about how the original shortcodes were produced.

#1874969

My issue is resolved now. Thank you!

TBH I couldn't find very much helpful information on displaying custom fields from intermediary post types, nor could I locate anywhere in the GUI for generating said shortcodes. I bookmarked a few articles in my search and I hadn't noticed (until you pointed it out) one had the .association identifier https://toolset.com/forums/topic/displaying-intermediary-post-fields-in-content-template/ (2017) and the other (2020 + a post answered by you) provided the .intermediary identifier https://toolset.com/forums/topic/trying-to-understand-the-intermediary-relationship/

I also noticed a few similar articles link through to https://toolset.com/course-lesson/many-to-many-post-relationships/ so adding a link (to somewhere where displaying custom fields for intermediary post types is covered) or adding some additional information to this page for others would probably be useful moving forward.

Either way I appreciate your help in getting the matter resolved, tyvm.

#1874973

Okay interesting, I did a search and found the documentation related to this .association identifier. I see why I've never used it before, and honestly I don't think it should ever be used in a realistic M2M/intermediary View scenario: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/

In a realistic scenario, most M2M relationships can have more than one related post from the other post type, so you'll need a View to loop over the related posts. When displaying the intermediary Types field in a View of related posts, you should always use the .intermediary identifier to prevent ambiguity.

I'm updating the resolution summary of that ticket to help prevent this confusion in the future.