Skip Navigation

[Resolved] Trying to understand how to display field from intermediary post in M2M relationship

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

Problem: I have a many-to-many post relationship that uses intermediary posts. On the single child post page, I would like to display a custom field from the intermediary post using the Types field shortcode, but it doesn't seem to be working correctly.

Solution: Since it's a many-to-many relationship, you may have more than one intermediary post related to the child post (or parent post). Since more than one result can be returned, you must use a View of the intermediary post type with a post relationship filter in order to determine which intermediary post you want to display. Then place the field shortcode in the loop of the intermediary post View. If the field is in the intermediary post, no item attribute is necessary. If the field is in the parent post, you must use the @relationship-slug.parent syntax in the item attribute.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/

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

Last updated by AlimB3245 4 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1799317

Tell us what you are trying to do?
I have a types shortcode that works well in a custom function

[types field='eyewear-store' output='raw' item='@brand-distributor-eyewear-brand.intermediary'][/types]

And I am trying to update the function but first I need to understand what the above types field outputs.. it's used in the function in a view that shows brand owners (brand distributors) and is connected to the eyewear-brand with the relationship brand-distributor-eyewear-brand.. now I know there's an intermediary post but I've never used it before.. so I am aware of how to access the parent or the child but I've never used an intermediary so I'd like to know if I needed to display the above result, should I use it on an eyewear-brand post or on a brand-distributor post.. I am a bit confused..

Along with your advise, would appreciate some links to know more about how to understand the intermediary relationship and its usage.

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site? hidden link

#1800347

Nigel
Supporter

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

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

There isn't a great deal of documentation specifically about this, they are covered in the many-to-many relationships documentation: https://toolset.com/course-lesson/many-to-many-post-relationships/

If I can use a common example not specific to your site, connecting bands and music festivals in a many-to-many relationship.

Many bands can appear at a music festival, and the same band can perform at many festivals.

There will be some data that is specific to a particular performance of some band at some festival, for example what time, which stage etc.

If Radiohead at to appear at Glastonbury at 9pm, you can't store that time as a field of Radiohead (they won't start all their gigs at 9pm), and you can't store that time as a field of Glastonbury festival (not all bands will be performing at 9pm).

You need somewhere that is specific to Radiohead appearing at Glastonbury to store the fields, and custom fields are stored as post meta, which means they belong to a post, so the solution is to create an intermediate post whose sole purpose is to store such fields.

When you are outputting data (in a Content Template, in a View etc.) it can reference any other part of the relationship as the source post.

So on a template for bands used to show a Radiohead post, you could insert fields whose source if the related festival (i.e. Glastonbury) or you could insert fields from the intermediate post (the relationship post), and you can insert both in the same template. Using the Block editor you tell the dynamic source what post from the relationship to use as the source, and the same is achieved in the classic editor with shortcodes using the item attribute.

#1800429

Hi Nigel.:-)..

Thanks for the update..:-)..

I am clear now about the intermediary part.. but I would like to know if the same types field could be used in both (the child and the parent) content template with the sale result?

Considering that the intermediary is neither a child nor a parent but an intermediate connection, if I were to place this code below in brand distributor (parent) content template or eyewear brand (child) template, would I have the same result?

[types field='eyewear-store' output='raw' item='@brand-distributor-eyewear-brand.intermediary'][/types]

Please do advise.

Regards,
Alim

#1800475

Nigel
Supporter

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

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

Yes, that should work, either template will switch context to the intermediate post type when outputting the field.

#1800593

Hi Nigel

Thanks for confirming that.. and that's exactly what I thought too...

So I place this code (with the h1 tags to highlight them on the page) on the content templates for brand owners (it has the relationship brand-distributor-eyewear-brand with eyewear-brand) and on the eyewear brands

<h1>The eyewear store should display here : [types field='eyewear-store' output='raw' item='@brand-distributor-eyewear-brand.intermediary'][/types]</h1>

This is the link to the brand page

hidden link

I expected the output here to be 'in' and 'ae' as the 2 eyewear stores as it has 2 brand distributos

These are the links to the two distributors for this brand

hidden link

I expected the output here to be 'ae' as this is the brand distributor for UAE (ae)

hidden link

I expected the output here to be 'in' as this is the brand distributor for India (in)

You can see that the relationship is fine as the brands are being displayed in the Our Brands section and in the My Distributor section too

What am I doing wrong?

Please advise.

Regards,
Alim

#1803183

Hi Nigel

Just following up, if there's been any update on this...:-)..

Regards,
Alim

#1803249

Hi, Nigel is busy at the moment but I'd like to step in and help. Since each eyewear brand can be related to multiple brand distributors, there can be multiple intermediary posts related to the current eyewear brand. A single Types field shortcode will not display multiple results. Instead, you must create a View to loop over those related posts and display one eyewear store field per intermediary post. For example, in the eyewear brand post, you would place a View of eyewear distributors with a post relationship filter based on the brand-distributor-eyewear-brand relationship where the eyewear brand post is the same as the post where the View is being displayed. Then in the loop of this View, you should place your Types field shortcode with "item" attribute pointing to the intermediary post:

<h1>The eyewear store should display here : [types field='eyewear-store' output='raw' item='@brand-distributor-eyewear-brand.intermediary'][/types]</h1>

This would display one eyewear store field for each related brand distributor post. If the eyewear brand has two related eyewear distributor posts, the View would display two eyewear store fields. If the eyewear brand has one related eyewear distributor, only one eyewear store field would be displayed. If no related distributors are found, no store fields will be displayed, and so on.

I hope this helps clarify. Any time it is possible to have more than one related post, you must use a View to display a Types field shortcode from those related posts. Otherwise, a single Types field shortcode will not help you display fields from multiple related posts.

#1803869

Hi Christian

Thanks for the update..:-)..

And yes, I guess I get it now.. but I always assumed that types would have shown the first one, even if there were multiple.. but I guess I was wrong..

I will have some queries regarding the usage of this but I think i'll create another ticket for that.

Have a nice day..:-)..

Regards,
Alim

#1803871

My issue is resolved now. Thank you!