Support Forum
Hi,
I would like to relate some Custom Post Types together and think that MetaBox (with a couple of its extensions) could do this very well.
Please could you explain the best way to use MetaBox and MB Custom Table to create a performant relationships field that will enable linking CPTs and their respective posts together in one-many and many-many relationships.
ACF has this functionality but the post relationships are not stored in a separate indexed table. Posts2Posts is designed to do exactly this but it is not easy to bring relationship management to the front-end as MB Frontend Submission could.
Thanks
Hi David,
Thanks for a great question.
Currently, the way Meta Box and extensions work are one-way relationship. You can use a field "post" to attach 1 or many posts to a post. That's similar to 1-1 and 1-many relationship, but just one way.
The MB Custom Table does the storing job only. So if you create a custom table with 2 columns ID and related_posts, you can store related posts for a single post. And you can query the related posts via helper functions.
I've been thinking about relationship, but I'm not too clear about the usage of that in the situation of WordPress. Would you mind describe what you need and let's work on a good solution?
Hi Anh,
In my latest project, I have several requirements for relationships so I'll give you one example which involves events and bands. To simplify I will not include all the custom meta for each custom post type, but will highlight the key elements that form the structure in CAPS:
CPT EVENTS with custom meta LOCATION, DATE, BANDS
CPT BANDS with custom meta TYPE, AVAILABILITY, EVENTS
The above shows how a custom booking system could allow site users to create an EVENT at a particular LOCATION which is available on a given DATE. Then they could book a BAND by TYPE of music that has AVAILABILITY on the chosen DATE.
The repeatable booking form would allow adding multiple BANDS to one EVENT creating a one-many relationship. In addition, the site would allow users to create a TOUR containing multiple EVENTS with multiple BANDS at each EVENT thus creating many-many relationships.
If people wanted to purchase tickets, they could search for EVENTS in their LOCATION and see what BANDS are playing on a given DATE, or they could search for BANDS they like and see what DATE they are playing near their given LOCATION (using FacetWP integration)
There are of course many other uses for relationships in WordPress for example:
CARS and PARTS - Many PARTS could fit many CARS
BOOKS and AUTHORS - Many AUTHORS could co-write many BOOKS
PERSON and PEOPLE - Social relationships between one PERSON or many PEOPLE
Hopefully this has given a clearer insight into my thoughts. Due to the complex nature of post relationship SQL queries, a custom table is essential to avoid hundreds of self joins and ensure efficient indexing, scaling and flexibility.
I'm happy to help with this - it's something I previously developed as a bespoke solution, but will now be using extensively within the WordPress ecosystem. A good post relationship extension for MetaBox would certainly make it the best CMS framework plugin for WordPress.
David
Hi David,
Thanks a lot for your examples. That makes the picture a lot clearer. I will read more about this (probably Posts 2 Posts is the best one in WP ecosystem, right?). In the mean time, would you mind sharing how you have implemented a solution before?
Hi Anh,
The implementation was for a previous client using .NET and MsSQL so it's not an identical comparison, but I can explain the principles which remain the same.
The two relationship tables were relatively complex but allowed for all conceivable searches to be performed extremely quickly (with only one join) and covering indexes which enabled the system to scale past millions of records with no noticeable performance degradation.
Relationship Structure table:
tId - Unique table ID
relationshipType - Type of relationship (1-1,1-Many,Many-Many)
contentType - Type of content (event)
contentId - ID of the content
relatedContentType - Content type of related content (band)
relatedContentId - ID of the related content
Relationships table:
tId - Unique table ID
contentId - ID of the content
relatedContentId - ID of the related content
biDirectional - Whether relationship is bi-directional (boolean)
I hope the above is enough for you get a clearer understand of how we achieved content relationships. Posts2Posts is certainly the best relationships plugin for WordPress (so far) but it is no longer supported and is missing advanced relationship/front-end capabilities.
David
Hi David,
Thanks a lot for your reply. I've looked at the database structure of Posts2Posts and it's quite similar to your 2nd table structure. I will try to implement this feature as soon as I can.
Thanks,
Anh
Hi David,
We have done the first version of the MB Relationships plugin. We hope that can solve your problem. Please try it and let me know if you need anything.
Hi Anh,
This looks like a great implementation of WordPress relationships. I'll give it a try soon and feedback anything potentially useful.
Best