MB Relationships is a WordPress plugin that helps you create many-to-many relationships between posts, pages, custom post types, terms and users. The plugin is lightweight and optimized for database and query performance.
The plugin allows you to create connections from posts to posts, posts to users and so on. Then you can perform corresponding queries to retrieve posts (or terms, users) that are connected to or from given posts (terms, users).
The plugin also supports reciprocal and bi-directional relationships.
Why Do You Need Posts To Posts Relationships In WordPress?
Post relationships is a missing part in WordPress. The only “built-in” way that mimic the post relationship in WordPress is the
post_parent for pages where you can create many child pages of a page (a one-to-many relationship). Unfortunately, that’s available for pages and hierarchical post types only. Besides, it’s not many-to-many relationship.
Below are some examples of posts to posts relationships that might help you see the benefit of this feature:
Creating Related Posts In WordPress
The simplest example is to manually create related posts in your WordPress website. When you edit a post, you can select posts that have similar or related content and display them in the frontend for further reading.
You can also query backward: displaying posts that link to the being read post as a reference to provide more information to your readers. With this, you don’t need a WordPress related posts plugin anymore.
Example: Events And Bands
Suppose you have two custom post types, event and band, where:
- In each event there may be multiple bands, and
- Each band can participate in many events.
The relationship is described in this diagram:
If people want to buy 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 location.
In this example, we have created many-to-many relationships between events and bands.
Creating Relationship Between Posts and Terms, Users
Then you can create a lot of relationships that fit your needs, for example:
- Connect products (custom post type) and distributors (user), where each product can be distributed by multiple distributors and each distributor can distribute multiple products.
- Connect singers (user) and songs (custom post type), where each singer can sing many songs and each song can be sung by many singers.
MB Relationships allows you to create bi-directional connections. You will be able to query back and forth without any problem.
The data is stored in the database as a pair of (from_id, to_id), thus making it independent from either side.
Besides, for each side, there’s a meta box that shows what are connected from/to. So you don’t have to worry about the direction of the connection anymore.
If you have built the relationships with field
taxonomy_advanced, you will see the difference here. The old way is clearly just one-directional relationship from an object type to another. It will be so hard to query or store the items that connect to a specific post.
An Alternative For The Posts 2 Posts Plugin (P2P Plugin)
Posts 2 Posts is a great plugin for creating relationships between posts and users in WordPress. It has been in the WordPress community for years and is used in thousands of websites.
We have been receiving suggestions from our users to create such a plugin on the foundation of the Meta Box. So we create MB Relationships to serve this need. Because the plugin is based on the Meta Box, its code is very lightweight and fast. Almost all the work in the admin area is done by Meta Box. This plugin only performs the connection tasks and handles queries for related objects on the frontend.
Inspired by Posts 2 Posts, we have built MB Relationships with a similar code base and API. MB Relationships is kind of a combination between Meta Box and Posts 2 Posts.
Inherit the strengths of both Meta Box and Posts 2 Posts, MB Relationship is lightweight and has a flexible API.
- Simple APIs: the plugin provides simple APIs for registering relationships and retrieving connected items. It integrates with existing WordPress APIs such as
get_users. See documentation for more information.
- Uses custom relationship table to store relationships. That helps optimize the database storage and query performance.
- You can create relationships between any kind of content in WordPress: posts to posts, posts to users, etc.
- Supports creating reciprocal relationships (posts-posts, users-users, …).
- Supports creating bi-directional relationships and easily query them.
- Display connected items easily with shortcode.
- Extremely lightweight and fast.