Continuing the series on creating a relationship between two objects on your website, we have just explored a different method, using Meta Box and Breakdance. In this practice, we’ll create a bi-directional relationship between Courses and Instructors post types and show them on both singular and archive pages.
This is an archive page for example. It includes a list of courses and the name of instructors who contributed to each course.
This is a singular page that shows information of an Instructor. There is also a section to display all the courses contributed by this instructor.
Video Version
Before Getting Started
We have two separate custom post types: Courses and Instructors. In this case, the relationship is to link the courses to their instructors, and also link each instructor to the courses to which they contributed. This bi-directional relationship will be shown on the both singular page of each post type and the archive page for Courses.
So, we need the Meta Box core plugin to have a framework to create custom post types, custom fields, and relationships. You can download it directly from wordpress.org.
Moreover, we need some Meta Box extensions for the advanced features:
- MB Custom Post Type: to create custom post types for the courses and instructors;
- MB Relationships: to create relationships between these post types;
- Meta Box Builder: to have an intuitive UI on the backend to create custom fields and relationships;
- MB Admin Columns (optional): to display the related courses and related instructors in the dashboard.
You can install these extensions individually or use Meta Box AIO to have them all.
The last one is Breakdance to build the pages.
Create the Custom Post Types
Go to Meta Box > Post Types to create a new post type for the instructors, and another one for courses.
After publishing, you will see new menus displayed. These are my created post types.
Create Custom Fields
If you want to add some custom fields to save some extra information for each post type, just go to Meta Box > Custom Fields to create fields as usual. In this practice, I just add some fields for the courses.
After creating all the wanted fields, go to the Settings tab, set the Location as Post type, and select the post type you want to apply the fields to.
Create Relationships
Go to Meta Box > Relationships to create the relationships between the Courses and Instructors post types.
There’ll be two sections: From and To with the same structure of settings.
Because we’re setting the relationship between 2 post types, set the Object Type as Post in both two sections.
In the Post type option, choose the post type you want to create a relationship. The relationship is bi-directional, so you can put the post types not in order. For example, I set the Instructor in the From section, and the rest is Course.
Because I activated the MB Admin Columns extension before, I have the following option in both two sections of the relationship’s settings.
It will help to display the related posts from the relationship on the dashboard like this.
In the Field tab, you can set the label for the relationship section in the post editor.
After publishing the relationship, you will see a new field on the right sidebar in the post editor of each post type.
In the post editor of the Course post type, you will see this one to choose which instructor is related to the current post. You can select several instructors in this section.
It’ll be the same when you add related courses for an instructor.
In the dashboard, add related courses for an instructor like this.
Create a Global Block for Courses
I will create a section to show all the information of the courses that the instructor contributed to. To display information about each course, we should create a global block.
Go to Breakdance > Global Blocks.
Select Div element to cover the whole block.
Add an Image element to show the course image.
Click on the Insert Dynamic Data button to insert dynamic data to this element.
Then choose Featured Image in the Post section.
The name of the course is the post title and embedded link, so add the Text Link element.
Then insert dynamic data from the Post Title.
The Text Link element also provides a setting to set the embed link as dynamic data.
And choose Post Permalink inside.
I’ll add a Div element for each kind of information that is from custom fields.
Go ahead, add a Text element for showing the first course’s information.
This course’s information is saved in a custom field created with Meta Box, so also insert dynamic data to the Text element. Look for the Metabox section, choose the name of the custom field.
For other lines, in the same way, just duplicate this Div element, change each Text element inside to get the right name and data from custom fields.
You can change those elements to style this block a little bit to have a good look.
Display the Relationship
In this practice, we’ll display this relationship on the singular page of the Instructor post type; and on the archive page where all the courses are listed.
Display the Relationship on the Singular Page of Instructors
This is an example of the Instructor post type. It shows detailed information about one instructor. There will be a section to show all the courses that the instructor contributed to. and, we already have a global block to display each one of those posts.
Go to Breakdance and create a new template for the page.
Remember to set the location for this template as the single page of the Instructors post type.
Add a Section element to cover all the content of the page.
Add the Post Title element to display the instructor name.
Then, add the Post Content element to get the instructor’s description.
For the section displaying all the contributed courses, there are multiple posts so we should add the Post Loop Builder element.
Choose the name of the created global block.
Since we haven’t set the source of data to query from, there will be no information display.
To display the course’s information, move to the Query section of the Post Loop Builder element, choose Array.
We should add some lines of code like this:
return [ 'post_type' => 'course', 'posts_per_page'=>-1, 'relationship' => [ 'id' => 'instructor-to-course', 'from' => get_the_ID() ], ];
'post_type' => 'course',
This line code is to stipulate that we will query posts from the Course post type.
'relationship' => [ 'id' => 'instructor-to-course', 'from' => get_the_ID() ],
These lines are to stipulate that only the posts that are set in the relationship will be queried. instructor-to-course
is the ID of the created relationship.
After adding the query, you will see a list of courses displayed automatically.
Just style this template now to get a better display.
Display the Relationship on an Archive Page
We’ll create an archive page showing all the courses. There will be the same kind of information for each course should be displayed, so you should create a global block for them.
In that global block, I also display the related instructor names of the course.
There may be more than one instructor contributing to a course, so we should use a global block to get and display their names.
So, there will be two new global blocks:
Display the Instructor’s name
Instructor Block is to display the instructor name only, so add a Text link element.
Click on the Insert Dynamic Data button to insert dynamic data from the Post Title. Insert link as well from the Post Permalink.
Next, we will add the instructor name from the created global block to the block for displaying course information.
This block to display the course on the archive page has the same structure and content with the one for the course on the Instructor singular page, so just copy all the elements from the previous block, and paste to this new one.
In the new global block for the courses, I’ll add a new Div element to display the instructor's name. Or, just duplicate the Div element to inherit its style then change the text.
Add the Post Loop Builder element inside the new Div.
Choose the global block as the one we created for the instructor.
We also need to regulate the query for this element. Choose Array, and add code.
return [ 'post_type' => 'instructor', 'posts_per_page'=>-1, 'relationship' => [ 'id' => 'instructor-to-course', 'from' => get_the_ID() ], ];
These lines are the same with the one we use to display the related course in the previous step. Just replace the ID of the post type with the instructor
.
This preview post has names of two instructors, so we should style this global block a bit more.
Now, it looks like this.
Create the archive page
Now, go to create the archive page then edit it in Breakdance.
Add a Section element to cover the page.
Inside the section, add the Heading element to the page title.
For the list of courses, use the Post Loop Builder again. Set the global block as Course Archive Block that we use for the courses displaying on this archive page.
And set the query for it. Just replace the post type with course
.
Now, customize the layout. Then you will have the best archive page.
We’ve finished.
Last Words
It's easy to create a relationship by using Meta Box and Breakdance. Furthermore, Breakdance supports creating related contents in an effortless way. So, let’s give it a try and share the results with us.
Hopefully, this tutorial will make sense to you. If you are looking for creating a relationship with another page builder, please look for it in this series.
If you have any suggestions for future tutorials, feel free to leave a comment below. Thanks for reading!
- How to Create Relationships - P1 - Using Meta Box and Oxygen
- How to Create Relationships - P2 - Using Meta Box and Bricks
- How to Create Relationships - P3 - Using MB Views
- How to Create Relationships - P4 - Using Meta Box and Breakdance
Other case studies you might be interested in
- Create A Dynamic Landing Page in WordPress Using Custom Field
- Create a Filter to Find Hotels by Location
- Create an OTA Website Like Booking.com with Meta Box Plugin - P1: Create a Page to Introduce Hotel Rooms
- Create an OTA Website Like Booking.com with Meta Box Plugin - P2: Create Filters on the Archive Page
- Create an OTA Website Like Booking.com with Meta Box Plugin - P3: Create Filters for Single Hotel Pages
- Create Dynamic Favicon in WordPress using Meta Box plugin
- Create Posts Series in WordPress Using Meta Box
- Display The Latest Products Section - P2 - Using Meta Box and Elementor
- Display The Latest Products Section - P3 - Using Meta Box And Oxygen
- How to Add Custom Fields for WooCommerce - P2 - Using MB Views
- How to Add Custom Fields to Display Banners using Meta Box Plugin
- How to Add Guest Authors and Guest Posts - Using Meta Box
- How to Add Related Posts - Using Custom Fields
- How to Build a Hotel Booking Website Using Meta Box - P1
- How to Build a Hotel Booking Website Using Meta Box - P2 - Booking Page in Backend
- How to Build a Hotel Booking Website Using Meta Box - P4 - Booking Management Page
- How to Build a Hotel Booking Website Using Meta Box – P3 – Booking Page for Customer
- How to Create a Classified Ads Website using Meta Box
- How to Create a Custom 404 Page in WordPress - P1 - Using Meta Box and Elementor
- How to create a FAQs page - P5 - Using Meta Box and Breakdance
- How to Create a Product Page - P2 - Using Meta Box and Oxygen
- How to Create a Product Page - P3 - Using Meta Box and Bricks
- How to Create a Product Page - P4 - Using Meta Box and Elementor
- How to Create a Product Page - P5 - Using Meta Box and Gutenberg
- How to Create a Product Page - P6 -Using Meta Box and Breakdance
- How to Create a Product Page - P7 - Using Meta Box + Kadence
- How to Create a Product Page - P8 - Using Meta Box and Brizy
- How to Create a Product Page - P9 - Using Meta Box and Divi
- How to Create a Product Page using Meta Box Plugin
- How to Create a Recipe - P2 - Using Meta Box and Oxygen
- How to Create a Recipe - P3 - Using Meta Box and Elementor
- How to Create a Recipe - P4 - Using Meta Box and Bricks
- How to Create a Recipe - P5 - Using Meta Box and Zion
- How to Create a Recipe - P6 - Using Meta Box and Brizy
- How to Create a Recipe - P7 - Using Meta Box and Breakdance
- How to Create a Recipe - P8 - Using Meta Box and Kadence
- How to Create a Recipe - P9 - Using Meta Box and Divi
- How to Create a Recipe with Meta Box Plugin
- How to Create a Simple Listing - P2 - Using Meta Box and Bricks
- How to Create a Simple Listing - P3 - Using Meta Box and Breakdance
- How to Create a Simple Listing - P4 - Using Meta Box and Elementor
- How to Create a Team Members Page - P1- Using Meta Box and Elementor
- How to Create a Team Members Page - P2 - Using Meta Box and Oxygen
- How to Create a Team Members Page - P3 - Using Meta Box and Bricks
- How to Create a Team Members Page - P4 - Just Meta Box
- How to Create a Team Members Page - P6 - using Meta Box and Breakdance
- How to Create a Team Members Page - P7 - Using Meta Box and Kadence
- How to Create a Video Gallery Page - P2 - Using Meta Box + Bricks
- How to Create a Video Gallery Page - P3 - Using Meta Box and Breakdance
- How to Create a Video Gallery Page - P4 - Using Meta Box + Elementor
- How to Create a Video Gallery Page - P5 - Using MB Views
- How to Create a Video Gallery Page - P6 - Using Meta Box and Zion
- How to Create a Video Gallery Page Using Meta Box + Oxygen
- How to Create ACF Flexible Content Field with Meta Box
- How to Create an Auto-Updated Cheat Sheet in WordPress
- How to Create an FAQs Page - P1 - Using Meta Box and Elementor
- How to create an FAQs page - P2 - Using Meta Box and Oxygen
- How to create an FAQs page - P4 - Using Meta Box and Bricks
- How to Create an FAQs Page - P6 - Using MB Views
- How to Create an FAQs Page - P7 - Using Meta Box and Divi
- How to Create an FAQs Page - P8 - Using Meta Box and Kadence
- How to Create an FAQs Page - P9 - Using MB Blocks
- How to Create an FAQs Page -P3- Using Meta Box
- How to Create Buttons with Dynamic Link using Custom Fields
- How to Create Category Thumbnails & Featured Images Using Custom Fields
- How to Create Download and Preview Buttons - P1 - Using Meta Box and Bricks
- How to Create Download and Preview Buttons - P2 - Using Meta Box and Oxygen
- How to Create Download and Preview Buttons - P3 - Using MB Views
- How to Create Download Buttons in WordPress - Using Custom Fields
- How to Create Dynamic Landing Page in WordPress - P1 - Using Meta Box and Elementor
- How to Create Dynamic Landing Page in WordPress - P2 - Using Meta Box and Bricks
- How to Create Menus for Restaurants - P1 - Using Meta Box and Elementor
- How to Create Menus for Restaurants - P2- Using Meta Box and Bricks
- How to Create Notification Using Custom HTML Field
- How to Create Online Admission Form for School or University
- How to Create Online Reservation Form for Restaurants using Meta Box
- How to Create Relationships - P1 - Using Meta Box and Oxygen
- How to Create Relationships - P2 - Using Meta Box and Bricks
- How to Create Relationships - P3 - Using MB Views
- How to Create Relationships - P4 - Using Meta Box and Breakdance
- How to Create Taxonomy Thumbnails & Featured Images - P2 - Using Meta Box and Oxygen
- How to Create Taxonomy Thumbnails & Featured Images - P3 - Using Meta Box and Bricks
- How to Create Taxonomy Thumbnails & Featured Images - P4 - Using MB Views
- How to Create YouTube Video Timestamps on WordPress Website - P1 - Using MB Views
- How to Display a Video Playlist - P1- Using MB Views
- How To Display All Listings On A Map With Meta Box
- How to Display Author Bio in WordPress - P1 - Using Meta Box and Bricks
- How to Display Author Bio in WordPress - P2 - Using MB Views
- How to Display Dynamic Banners in WordPress - P3 - Using MB Views
- How to Display Images from Cloneable Fields - P1 - with Gutenberg
- How to Display Images from Cloneable Fields - P2 - Using Meta Box and Oxygen
- How to Display Images from Cloneable Fields - P3 - with Elementor
- How to Display Images from Cloneable Fields - P4 - with Bricks
- How to Display Opening Hours for Restaurants - P1 - Using Meta Box + Gutenberg
- How to Display Opening Hours for Restaurants - P2 - Using Meta Box and Oxygen
- How to Display Product Variations - P1 - Using Meta Box and Gutenberg
- How to Display Product Variations - P2 - Using Meta Box and Oxygen
- How to Display Product Variations - P3 - Using Meta Box and Bricks
- How to Display the Dynamic Banners - P2 - Using Meta Box and Bricks