In the previous part, we used PHP to display related posts based on a shared relationship. This time, we’ll do the same thing, but with the MB Views extension of Meta Box instead. This can be applied to various scenarios, such as showing related events connected to the same artists, related products within the same category, or additional articles by the same author.
We will create a bi-directional relationship between events and artists using another extension from Meta Box - MB Relationships. Then, show events linked to the same artist on a single event page like this.
Video Version
Before Getting Started
In this practice, we highly recommend using Meta Box AlO. This premium version includes the framework and all essential extensions, giving you quick access to the advanced features we’ll use.
Here are some needed extensions:
- MB Custom Post Type: to create custom post types for the events and artists.
- MB Relationships: to create relationships between these post types.
- MB Views: to create a template for displaying related posts based on a relationship.
- MB Admin Columns: to display the related events and related artists in the dashboard. It’s just optional.
- MB Builder: to have the UI create the relationship in an intuitive way. In addition, if you want to add extra information about the events and artists, you also can use this extension to create custom fields to store that kind of data. In this practice, I don’t use it to create custom fields to make it simpler.
Let’s start now.
Create New Custom Post Types
Go to Meta Box > Post Types to create a new custom post type for the artists and another one for events.
After publishing, you’ll see the created custom post types in the admin dashboard.
Now, we can add some posts for each post type.
Then, let's create a relationship between the events and artists.
Create the Relationship
Go to Meta Box > Relationships to create the relationships between the Events and Artists post types.
You can enter all the settings for the relationship and each side of it in the From and To sections.
Because we’re setting the relationship between two post types, set the Object type as Post in both sections.
In the Post type option, choose the post type you want to create a relationship with. The relationship is bidirectional, so you can put the post types not in order. I set the Events in the From section, and the rest is Artists.
Next, this setting is available when you activate the MB Admin Columns extension only.
Once enabled, columns will appear in the dashboard showing related events and artists.
In the Field tab, you can set the label for the relationship section that shows in the post editor.
There’ll be a box at the right sidebar to choose which Artists are related to the current post in the Events post type, and vice versa.
After publishing, go to a post editor; you can see a section like that allowing you to select several artists.
Here is my example of filling out all relationship information for events and artists.
This is my single event page before this practice.
Now, I will add other events that the artist of this event also participates in, like I showed at the beginning. I named this section as Related Events.
Now, head over to Meta Box > Views, and create a new template specifically for this purpose.
With MB Views, you can add some lines of code directly to do what you want.
Here is my example code:
<h2 style="font-weight:bold;">Related Events</h2> {% set artist_related = { relationship: { id: 'event-to-artist', from: post.ID }, post_type: 'artist' } %} {% set artists = mb.get_posts(artist_related)|reverse %} {% set unique_events = [] %} {% for artist in artists %} {% set event_related = { relationship: { id: 'event-to-artist', to: artist.ID }, post_type: 'event' } %} {% set events = mb.get_posts(event_related)|reverse %} {% for event in events %} {% if event.ID not in unique_events and event.ID != post.ID %} {% set unique_events = unique_events|merge([event.ID]) %} <li><a href="{{ event.url }}">{{ event.post_title }}</a></li> {% endif %} {% endfor %} {% endfor %}
Explanation:
<h2 style="font-weight:bold;">Related Events</h2>
This line sets the title for the section.
{% set artist_related = { relationship: { id: 'event-to-artist', from: post.ID }, post_type: 'artist' } %}
We create the artist_related
variable to query posts of the Artist post type that are related to the current post via the relationship ID (event-to-artist
).
{% set artists = mb.get_posts(artist_related)|reverse %}
Then, use the Meta Box function (mb.get_posts
) to retrieve the related artists and apply the reverse
filter to change their order. And the result is saved into the artists
variable.
{% set unique_events = [] %}
This line helps to initialize an empty array, avoiding showing duplicate events later.
Loop through each artist in the list.
{% for artist in artists %}
Inside the loop, we create a new event_related
variable to get all events connected to that artist, as you can see in the code below.
{% set event_related = { relationship: { id: 'event-to-artist', to: artist.ID }, post_type: 'event' } %}
Similarly to how we retrieved the artists earlier, we now use the following code to get the events related to each artist, reverse their order, and store the result in the events
variable.
{% set events = mb.get_posts(event_related)|reverse %}
Now, we go deeper into another loop to go through each related event.
{% for event in events %} {% if event.ID not in unique_events and event.ID != post.ID %} {% set unique_events = unique_events|merge([event.ID]) %} {% endif %} {% endfor %}
It will check if the event is not the current one and hasn’t already been added to our list. If both conditions are met, this line will help to add the event to the list.
And this line below displays the event as a list item, with a clickable link to the event page and the event’s title.
<li><a href="{{ event.url }}">{{ event.post_title }}</a></li>
That’s all for the code.
Now, just move to the Settings section of the view, set the Type as Singular, choose the Location as the post type where you want the event information displayed.
Go to the frontend, related events appear under each single event post. It also works well when you check on another post.
Last Words
Connecting related posts through a shared relationship, like events featuring the same artist, helps your audience discover more of what they’re interested in. If you’re looking for an easy way to display related content using bi-directional relationships, take a look at this guide for detailed instructions. Thanks for reading!
- How to Display Related Posts Based on a Shared Relationship with Meta Box
- How to Display Related Posts Based on a Shared Relationship - P2 - Using MB Views
- Author Bio
- Better 404 Page
- Blogs for Developers
- Building a Simple Listing Website with Filters
- Building an Event Website
- Building Forms with MB Frontend Submission
- Coding
- Create a Chronological Timeline
- Custom Fields Fundamentals
- Design Patterns
- Displaying Posts with Filters
- Download and Preview Buttons
- Dynamic Banners
- Dynamic Landing Page
- FAQs Page
- Featured Products
- Full Site Editing
- Google Fonts
- Gutenberg
- Hotel Booking
- Latest Products
- Logo Carousel
- MB Builder Applications
- MB Group Applications
- MB Views Applications
- Most Viewed Posts
- Opening Hours
- OTA Website
- Pricing Table Page
- Product Page
- Product Variations
- Querying and Showing Posts by Custom Fields
- Recipe
- Related Posts via Relationship
- Restaurant Menus
- SEO Analysis
- Simple LMS
- Speed Up Website
- Taxonomy Thumbnails
- Team Members
- User Profile
- Video Gallery