Bidirectional post relationships in WordPress


As WordPress progresses further and further beyond its original scope as a blogging platform and well into the full blown content management system (CMS) realm, the need for more complex relationships between content increases.


WordPress provides powerful mechanisms built into the core for structuring content, namely custom post types and (custom) taxonomies, which you can utilise to create a myriad of structural patterns for your content, but sometimes it’s not enough.

What you can do with the core alone

Using custom post types WordPress allows you to create your own content types which can behave like and build upon any of the built in types WordPress uses including posts and pages. Thinking about content as different post types is very powerful as each type can be handled differently in terms of their data and how and where they are displayed on the site. If you’re a theme/WordPress developer and you’ve never used custom post types, you should definitely check them out. It’s one of the most powerful features of WordPress and will revolutionise the way you use WordPress.

Taxonomies, on the other hand, allow us to group and categorise content in WordPress. WordPress already comes with two default taxonomies, i.e. tags and categories, but custom taxonomies can be added easily. For example, a ‘genre’ taxonomy could be created for organising a book post type or a team member post type might be organised using a ‘role’ taxonomy

Why we need bidirectional relationships

While these mechanisms open a vast array of possibilities for structuring content there is one key relationship which they don’t cover, specifically: many to many relationships. There’s also one-to-one and one-to-many but since these are subsets of many-to-many and thus any mechanism which can achieve many-to-many can achieve both one-to-one and one-to-many we’ll stick to talking about many-to-many for ease and brevity.

In a nutshell, a many-to-many relationship is a pattern whereby any one entity can be directly linked to any number of other entities and vice versa. Let’s consider a real world example, continuing with the book example again: We can use taxonomies to categorise our ‘book’ custom posts into genres, but when it comes to authors we need many-to-many relationships because any one author can write multiple books and a book can be written by multiple authors.

Some other potential use cases include:

  • People and teams, particularly where individuals can be in multiple teams
  • Events and speakers
  • Products and case studies/guides which feature the products

You could pull off this pattern with post types and taxonomies alone but it’d be very unwieldy from both a development perspective and a blog user perspective. Recently we needed many-to-many relationships for a client project so we went in search for a suitable solution.

The solution

Frankly, we didn’t have to look very far. The solution comes in the form of an add-on for a very popular WordPress plugin: Advanced Custom Fields (ACF). ACF is a very powerful plugin which we have written about before in our post ‘Five WordPress plugins we couldn’t live without’. If you ask us, it should be part of the WordPress core and if you’re not using it, you should be.

The add-on is, appropriately, named ‘Bidirectional Post Relation Field’ (referred to as BPR Field from here onward) and does exactly what it says on the tin. The plugin is available on the WordPress plugin repo here but there is a more up to date version available on GitHub here (which has added support for ACF v5). Note: when using the plugin we found a bug specifically when using the ‘Object’ return type which we modified the plugin source to fix. Get our modified version here.

Using the field – Blog owners

The interface for the blog user(s) couldn’t be any simpler, our clients have had no trouble getting to grips with using it.

bidirectional

Setting up the field

Getting the BPR Field working is simple. Making sure you already have the ACF core plugin installed, you simply install the BPR Field as you would any other plugin. Plugin install instructions can be found here. If you download the plugin from either of the GitHub links above then you will have to follow the instructions for ‘Manual plugin installation’.

Once installed the field will be available in the list of fields when adding fields to a field group in ACF. I won’t go into detail about this as ACF already has excellent comprehensive docs which can be found here. If you’re linking together separate post types you just have to make sure the field is applied to both for it to work correctly.

In your templates the field can be retrieved as you would any other custom field and returns either the ID of the linked posts or post object depending on what you selected when setting up the field.

Conclusion

We couldn’t ask for a simpler solution to what could otherwise could be a complex, time consuming problem and it comes with a simple, effective user interface for blog users to boot. Hopefully, you find it as useful as we have.