Skip Navigation

[Resolved] Setting up proper relationships for multiple Custom Post types

This support ticket is created 7 years, 2 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 10 replies, has 2 voices.

Last updated by amrinderS 7 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#566222

I have built many to many relationships in the past and right now i have contacted to look for a better approach when there are more than 3 CPTs. I have already made it work, but i am making sure if i have followed a correct approach.

So, this is the scenario

Locations --> Parks --> Prices -->Manufacturers --> Models

Here, Parks price change by location and also by model .

Second thing is that Park will be added just once, and then need to show in different locations with different prices. For example if Park A is added, then it cannot be added again to create the relationships easier like assigning price differently.

So, what i did was made Locations and Parks as parents of Prices and was able to fetch different price by location easily.

Only problem is how could i vary price by manufacturer and model ?

However, i have made it work on my website hidden link but i am not sure if this is the right approach. hidden link

This is the screenshot explaining a bit hidden link

Let me know

#566342

Nigel
Supporter

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

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

Hi Amrinder

I see from your demo form for models that you have created post relationships where park, location, price, and manufacturer are all parent posts of models, but I just need to clarify from you how this all fits together to be sure of the best way to set this up.

You are offering static caravans for sale in different locations, yes?

And the same models may be for sale in different places and the price will be different depending on the model and the location, is that correct?

The model and the manufacturer are tied together in a one-to-many relationship (a manufacturer may offer several models, but any given model will always be produced by the same manufacturer).

The location (country) and park (place) also belong in a one-to-many relationship (parks belong to a specific country).

So the price really depends on two variables, the model and the park, is that right?

So I think you post relationships should look like this:

location      manufacturer
   \             /
   park      model
     \       /
       price

You set up your park/location relationships (Perpignan is in France) and your model/manufacturer relationships (Santana 2017 is made by Delta).

Then set up your price posts that connect a model with a park (e.g. Santana 2017 in Perpignan costs 25,495).

If there is something specific you need help with once you have the data set up then let me know.

#566357

Hello Nigel,

Thanks for your support.

Only one thing which i differ from is that " One park can be in different locations and also one location can have different parks with different prices for example if park A is in France, and also in Italy , then price is also set according to location."

"The location (country) and park (place) also belong in a one-to-many relationship (parks belong to a specific country)."

How is it one to many ?

Also, i can set up the data u want to look like after you go through this query of mine.

Thanks

#566379

I have setup the data as you have told. I am facing difficulty in fetching the data, maybe i am confused. As for now in the location view, hidden link i need to fetch the parks, models, manufacturer and price in a table grid.
As right now i am not getting, value for location and manufacturer.

Check the screenshot.

hidden link

This screenshot explaining the filter i used hidden link

This is the link hidden link

#566397

Nigel
Supporter

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

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

Hi Amrinda

I'm confused about your parks and locations.

In your example screenshot you show the park Perpignan, which is in the location France.

Perpignan will always be in France.

But from your last replies it sounds like the same park might be in different locations, is that right?

I'm not sure I understand how that could be possible. I can imagine that you might have two parks with the same name in different locations (in which case the parks would be two separate posts), but not one park in two different locations.

Before I go any further could you clarify that?

And then in terms of displaying the data, your objective is to show for a location all of the available models in that location, yes?

#566400

Hello Nigel,

I will explain this with an example:

Assume, i have added one Park and it is Park A.
It is in 3 locations, France--Italy--Greece
So, in 3 locations, its Price will be different. Let it be 30$--50$--70$

This is where i am confused now. The same Park will be having different manufacturers and Models as well in different locations and for each different Model, the price will be different.

It will be like in Location France

France--Park A --Manufacturer A---Model AA----50$
France--Park A-- Manufacturer A--Model AB--80$

Now same Park A in location ITALY

ITALY--Park A ---Manufacturer A--Model AA--40$
ITALY--Park A---Manufacturer A--Model AB--70$

NOTE: PARK A will be added just ONCE, not each time. I hope you get my issue, this time.

Thanks

#566403

Nigel
Supporter

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

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

Thanks for clarifying.

One more question. The same models are available at different parks with different prices?

So, to extend your example above you might also have

France--Park B--Manufacturer A--Model AA----55$
France--Park B--Manufacturer A--Model AB----85$

Italy--Park B--Manufacturer A--Model AA----45$
Italy--Park B--Manufacturer A--Model AB----75$

Is that correct?

#566405

Yes you are right.

#566416

Nigel
Supporter

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

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

In which case you have a many-to-many relationships between the park and the location, and you need a new intermediary post type (e.g. "park in location") to connect them.

The revised structure for your post relationships would then look like this:

park    location     manufacturer
   \     /            /
park-in-location    model
            \       /
              price

So you need to connect the parks and locations with the intermediate post type, e.g. create a "Park A in France" post, a "Park A in Italy" post, a "Park B in France" post, "Park B in Spain" post etc.

You then use these park-in-location posts to connect the price and model posts.

e.g. you make a "price of Santana 2017 in Park A in France" post that stores 50$ (you might want to store it in a numeric custom field on the price post type and use the post title to describe the connections as with the above string.

To display models for sale you will need nested Views, the outer one showing "park-in-location" intermediate posts for a given location, e.g. France, and an inner View which returns the price posts with a post relationship filter to show posts that are child posts of the current park-in-location post.

In the Loop Output sections you will be able to use the id attribute to print content from the parent posts (rather than the intermediate post types being queried by the Views).

To display the manufacturer from the price posts you would need to use the technique for displaying content of grand-parent posts as described here: https://toolset.com/documentation/user-guides/displaying-fields-grandparents/

You may, though, find it easier to make the manufacturer a custom field on the model posts, from your description I'm not sure you need to make manufacturer a full post type.

I hope that is helpful, but if you have problems implementing it do let me know.

#567832

Hello Nigel,

Due to some changes, now the relationships has changed.
one-many many-to-many
Locations---->Parks---->Park-Models<--------Models

For example: Lets assume there is a PARK A and it is in France then this Park cannot be in another Location.
One park can however have many models and one model can have many parks assigned to it.

I am bit confused and maybe not able to understand the documentation of grandparents and brother pages.

I need to display this like :

I am in a single page of location. For example: hidden link
I want to display it:

Park A ----Model A
Park A-----Model B
Park B---Model A
Park C----Model B

But i am getting like Park A---then all childrens if it like
Park A
--Model A
---Model B

I need to run it each time.

#568025

Thanks and i was able to figure out myself and NIGEL you have been of great help.