WordPress Relationships Explained

Lana Miro
Javarevisited
Published in
6 min readMay 25, 2022

What are WordPress relationships, and how to master them? These questions are seemingly easy, yet many first-time developers will probably fail to answer them. Today, I’ll peek out a curtain to make you see clear every little thing about relationships and their types.

First things first, you’ll need to set WordPress relationships up to create connections between different data items pertaining to a single WordPress site. Second, set the relationship type you prefer.

There are three main WordPress relationship types to choose from: one-to-one, one-to-many, and many-to-many. I’ll get to them shortly.

Third, a correctly set relationship brings a WordPress development project up a notch. As a website developer, you’ll be able to make dynamic changes to the site. It means that you can adjust the needed meta box data and custom fields in the admin panel, and the updates will appear everywhere in the front, at once.

wordpress relations types
Source: FAVPNG

Are you hooked already?

Keep reading to learn more about WordPress relationship types, their examples, and use cases.

One-to-One

It’s the simplest and the least interesting database relationship type. A one-to-one relation denotes a linear connection between two database records, two custom post types, two posts, etc. It is possible when there is only one parent post with one child post related to it and vice versa.

Generally, such data lands into one DB table and is stored in the same row. One-to-one relationships exist between:

  • post ID and post content;
  • post ID and post slug;
  • post title and post content;
  • user ID and username;
  • comment ID and comment content, etc.

One-to-One Relationship Use Case

Since one child post can relate only to one post parent type, there are few examples to cite. However, a link between a country and its capital is a one-to-one relationship because a capital city is always unique.

One-to-Many

One-to-many is the most-used WordPress relationship. It implies that one database record can relate to more than one other item at once. Simply put, one parent post type can be linked to many child post pages.

one to one vs one to many relations
Source: Medium.com

One-to-many relationship stretches over posts, as mentioned previously, post metadata, users, comments, and terms. Therefore, it’s vital to see the difference between post-involving relations and those not. As to WordPress relationships involving posts, they are as follows.

Posts-to-Posts

Typically, the posts 2 posts relationships cover the links between different post attributes and attachments from the same database table called wp_posts. It is a classy parent-child relationship when a generic post type has several children, including subsidiary posts, pages, and attachments.

Posts-to-Post metadata

One post can have many meta attributes like post ID, meta ID, meta key, meta value, etc. But these post metadata will be linked exclusively to their parent post.

Posts-to-Comments

Comments are located in the wp_comments DB table. A database table can store the comment ID, comment content, approval status, author details like name, email address, etc. Once again, the comments pertain to one given WP post and can be linked to the wp_users DB table, giving website developers the option to create connections between Users and Comments.

Posts-to-Users

Last but not least, there are the posts to users correlation. It is slightly different from the WordPress relationships mentioned above because the User is the post parent, not the other way around. Each WordPress User can write as many posts as desired, and these posts will be inevitably linked to the wp_users DB table via the user ID field.

I’ve enumerated the relationships between posts and collateral data. Nevertheless, you should know that there are other relations where posts are not involved.

  • Users-to-User metadata. It is a connection between two WordPress database tables — wp_users and wp_usermeta. One user can have several metadata records, whereas each metadata item will be associated only with that user.
  • Comments-to-Comment metadata. A relation is similar to the one described above. One comment from the WordPress website can have many related items, but those metadata will apply only to one comment.
  • Comments-to-Users. When a registered user leaves a comment on the website, the record goes straight to the wp_comments DB table. A connection that forms between the user and the comment is a one-to-may relationship since one user can leave as many comments as they want.

One-to-Many Relationship Use Cases

👨🏻‍💼 Agent & properties 🏡

Imagine you’ve got a real estate sales and rentals website. Several real estate agents advertise selected properties. One agent can be set to represent several projects at once, but more than one agent cannot represent one property.

🏋🏻‍♀️ Fitness coach & sessions 🏐

Speaking about a gym website, there can be multiple fitness coaches working there. One given fitness coach can host as many exercise sessions as possible per one week, which means a one-to-many connection.

Without a doubt, I could have cited more examples — the sky is the limit. I just hope you’ve grasped the idea since all the use cases will be primarily lookalikes.

Many-to-Many

Finally, we’ve reached the last checkpoint. Many-to-many relationships, aka bidirectional relationships, are patterns whereby any database record can be linked to many other records and vice versa. WordPress mainly uses it to relate taxonomy terms and posts.

One term can be assigned to multiple posts pages, whereas one post can have multiple terms assigned to it.

On the back end, there are not two but three database tables involved in the process. The first two are the actual data tables — like wp_posts and wp_term_taxonomy. The third one is a junction table storing the key data from both data tables.

Now, you have to really pay attention.

To link a post from the first table and a term from the second table, WordPress takes the needed post ID and term ID, putting them next to each other in the third table. That is what we call a record.

The junction table can register multiple records for both posts and terms, creating, therefore, bidirectional relationships.

WordPress term relationships example
Source: Envato Tuts+

As you can see from the example above, one post is linked to several terms simultaneously. Conversely, one term applies to multiple different posts.

That’s how many-to-many WordPress relationships work, in a nutshell.

Many-to-Many Relationship Use Cases

🗣 Speakers & events 🏛

Imagine you’ve got a meetup website where different speakers attend different events. One speaker can give a speech at a particular event while the same event is attended by several other speakers.

✍🏻 Author & books 📖

One book can be written by one author. The very same author can write many books. Another book can be written by several authors at once. When searching for it, you will inevitably stumble upon its author and co-author(s). Do you get the idea? 😉

one to one relations example
Source: Medium.com

⚙ Many-to-many is probably the most challenging relation type. To create many-to-many relationships, you’ll need a custom post types plugin that allows establishing bidirectional connections.

It can be MB Relationships by MetaBox, Bidirectional Post Relation Field by ACF, JetEngine Relations, and many more.

Final Thoughts

It’s a no-brainer to build WordPress relationships once you understand the logic behind them.

  • One-to-one is the simplest connection type that exists between two database items.
  • A one-to-many relationship is possible between a parent item and its child items. One parent can have multiple subitems, whereas children can only have one parent.
  • Many-to-many is the most complicated relationship type. It is a direct link between two database items going in both directions. In other words, one post can be connected to several terms, and the same terms can be related to that and other posts simultaneously.

Hope I didn’t cause any more confusion today. Quite the opposite; you’ve learned new things today and will no longer faint when hearing the sacred “WordPress relationships” words. Good luck!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Javarevisited
Javarevisited

Published in Javarevisited

A humble place to learn Java and Programming better.

Lana Miro
Lana Miro

Written by Lana Miro

I’m passionate about website design & development. Working as Content Manager at @Crocoblock. Here is my Twitter account: @Lana_Miro

Responses (4)

What are your thoughts?

Great Info!
Thanks for sharing such WordPress relationships information.
Very helpful.

Excellent article and I really appreciate the visuals. It makes a complicated subject easier to understand. Thanks!