Don’t Worry, You’re In The Right Place
A lot of these posts were originally on SureFireWebServices.com. So if you found them through search engines or clicking around, this is their new home :). Enjoy!

Many to Many Posts Made Easy with Types, Views, and P2P

Many to many posts is a common problem that I keep running into with Types and so do a lot of others.  The main question most people ask is “How the hell do you successfully run many to many posts with the Types and Views plugin?”  Well to be honest, I’ve been developing for quite some time and it’s still a huge freakin’ pain to do, but I finally found a great way that will make life SO much easier for you in the future.

Why is many to many post relationships such a problem.

Well, for starters,  by default Types only allows many to many relationships by hacking it.  It’s not directly built into the software (not sure why as it’s been requested for years now).  In their documents, they tell you that in order to make many to many relationship type posts, you need to add a third CPT (custom post type).

This is ridiculous because:

  1. It doesn’t really work in every situation.
  2. It’s incredibly sloppy.
  3. Who wants to manage a third post type?!

In order to manage these relationships, you have to create tertiary posts that link the 2 posts together.  However, if you have “many to many” which is what’s needed, then you have to create a new post for every single relationship. Cumbersome? Yes

Doesn’t Types have Parent / Child Relations? Can’t you use that?

No.

Here’s how parent / child relationships work in Types.

You have a house (Parent). That house has bedrooms(Child).

You can list as many bedrooms as you want in that house.

NOW, What if you have 2 houses that both have a living room.  Not uncommon I’d say.  Well, since each child post can only have 1 parent, you have to recreate that living room on the parent post.  You can’t reuse child posts that were already created.

If you’re situation is more complex then that, then that’s going to leave you with A LOT of child posts. Now add that tertiary post in, and you can see how this can start getting very very messy.

Introducing P2P

Now I’m pretty new with this plugin but I did some digging and it truly is the definition of many to many relationships.  I think the problem most people have is that it involves a little bit of code.  Don’t be scared though, it’s really easy.

If we go back to the house/room example, you can make the living room once, and then just link it to every house if you need without having to constantly recreate the same post over and over.  The same works vice versa, create a room, and link it to as many houses as you want.

The Code

Here’s the magic that puts it all together, and you can also find this on the P2P documentation page.

First you have to set up the actual posts that need to be related:

[gist id=”4987734d263072362ad9″ file=”p2p_setup.php”]

Then you need to display it

[gist id=”4987734d263072362ad9″ file=”display_p2p.php”]

In the above example I used WooCommerce, so this specific loop hooks into the product summary, but you can literally hook it anywhere you’re theme allows.

That’s it! Now you have access to all the posts in a clean simple format, and they’ll display however you choose.

In my next post, I’ll show you how to actually set up the Views plugin to display the related posts.

Advertisement
About the Author
What's UP! This is my site, I write 99% of the articles on here. I'm also the owner of SureFireWebServices.com. I help out a lot of developers and designers getting into the web game. Helping is fun for me, so feel free to ask me any questions! I've made courses and have a membership as well to help get you on your feet!
Scroll to top