Skip Navigation

[Resolved] One to many relationship, then to many relationship

This support ticket is created 6 years 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 6 years ago.

Assisted by: Nigel.

Author
Posts
#1162502

I have 3 post types:
- Distilleries
- Spirits
- Cocktails
The first relationship is distillery to spirits (one to many) One distillery can have many spirits (gin, vodka...)
The second relationship is spirits to cocktails. One spirit can have many cocktails (3 different cocktails made with Distillery A's gin, for example).

What I want: to find out how I can then connect cocktails to distillery to:

- create a page where I show Single Distillery's info. Then, a list of spirits they have. Easy part, already working.
But can I also show all cocktails made with any spirits from this distilery - made with gin, vodka etc from that one distillery?
If so, then I don't understand what to do with the relationships... Do I create a new one ? How do I connect them?

- also create a page for Single Cocktail. Infos about the cocktail, then which spirit it is made with (with link to Spirit Single) and from which distillery (with link to Distillery Single). Is that another relationship?

Thank you for explaining how I can achieve this

#1162760

Nigel
Supporter

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

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

Hi Mylene

With extended relationships such as these, you go down the tree by creating Views for each generation, and up the tree by using Content Templates to force the required context for each generation.

So, if you want to display related cocktails and the starting point is a distillery, you create a View to display the spirits related to that distillery (using a Query Filter to set the distillery according to the post where the View is shown). The output section will loop over the matching spirits. You don't want to output anything from the spirits themselves, but they will provide the context for a second, nested, View which you insert between the wpv-loop tags of the first. This second View will display cocktails, and will include a Query Filter to set the parent spirit according to the current post in the loop (i.e. of the parent View). The output section will output fields from the cocktail posts.

You will probably want to delete the default "No items found" text from both Views.

Going in the other direction, starting from cocktails and traversing up towards distilleries, the first step is simple.

When you insert a field—e.g. the post title—using the Fields and Views button, there is a Post Selection tab where you can specify that the field should be displayed for a post other than the current post, including from a related post, which is how you display spirit posts when on a cocktail post. (An 'item' attribute is added to the shortcode that outputs the field, specifying the alternate source.)

You employ the same technique on a spirit post to display a field from the distillery. The problem is that your starting point is not a spirit post, but rather a cocktail post, and so for this to work you must first force the context from a cocktail to the parent spirit, using a content template.

That is described here: https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/displaying-fields-of-grandparents/

In brief, you create a content template to output the required fields of the distillery (even it is just the distillery name, or post title). You must not assign this template to anything, but create it as if it were used to display spirit posts.

So if you insert the post title using the Fields and Views button, you would use the Post Selection tab to specify the distillery post related to this spirit, which will add an item attribute to the shortcode, in the format item="$relationship-slug.parent".

Now go back to the template for cocktail posts where you want to display these fields from the distillery grandparent, and use the Fields and Views button to insert the new content template you just created (which adds a wpv-post-body shortcode).

The Post Selection tab is missing when inserting a content template (we have an internal ticket about this), and you will need to manually add the item attribute yourself, using the same format, but this time the relationship will be the spirit-cocktail relationship. What this is doing is setting the context for that content template to be the parent spirit (rather than the cocktail itself), and the fields in that template take that as the starting point for getting from the spirit to the distillery.