Black Friday Sale
Upto 40% OFF
Discount Auto Applied
Days
Hours
Seconds
The ACF Relationship Field is excellent for creating advanced relationships between one post type to another (like from A–> B). You can create relationship fields to link posts with common roles and characters. They are pretty amazing when you want to relate one post type, a page with another post type, and page. The ACF Relationship field provides a dual-column component to select one or more items. It allows you to filter your results using a search bar and via post types and taxonomies.
Additionally, using the ACF Relationship Field, we can create a bi-directional relationship (B<– A) between posts; however, there doesn’t seem to be any direct support by ACF for this. The only way you can accomplish this would be by writing some code, and that’s not something everyone wants to do.
However, you can easily implement this kind of relationship by using our plugin called AnyWhere Elementor Pro. This plugin allows you to get post data from the direct ACF Relationship field and reverse relationships without writing any code. All you need is our plugin and Elementor Page Builder installed on your WordPress site with access to the ACF fields, of course!
As the ACF Relationship & Reverse Relationship implementation can be a little tricky, I have opted for a use case for your better understanding.
Directly jump to Reverse Relationship implementation.
UseCase
Suppose we are creating a movie listing website with two post types: Movies and Cast. Some custom fields are assigned to the Movie post type, like release date, duration, and an ACF Relationship Field type “related cast.” The Movie detail page will display information related to the movie, also the Cast detail that has worked on it based on the Relationship field type we have created.
The post under the Cast post type will give information about the actors, including name, image, and list of movies they have acted in. The relationship between Actors and Movies is achieved using a reverse relationship as there is no direct relation b/w Cast and Movies. Later in this tutorial, you will see how we can get posts data by applying Reverse relationships.
I created two post types, Movies & Cast, using any plugin; I recommend using the Custom Post Type UI plugin. Next, add some posts to both the post types.
After assigning custom Fields to the Movie post type, we need to populate these fields for individual posts.
At this point, we are finished with the basic ACF settings. In the following tutorial, we will be displaying data from custom post types on the front-end via a Single post template for both Movies and Casts. Additionally, a block layout is required that will help render posts in templates.
Before heading towards creating post templates, let’s first create the block layout in the AE Template.
Start with creating the block layout as needed on both Movie & Cast single post templates.
You can design separate layouts for both templates; it’s completely up to you. Learn more about creating a Block Layout in AnyWhere Elementor Pro.
Here, we will create a single post template for the Movies post type, including the details like release date, rating, director, and the cast contributed to the movie.
Create a new AE Template (AE Templates -> Add New) with the following configurations.
To design the “Movie Detail” page, we must obtain data from custom fields. To do this, I have used the AE- ACF Field widget to show all of the data present in custom fields. The screenshot below shows the custom field data.
After displaying data from the other custom fields on the frontend, the next step is to display data from the ACF Relationship field (“Related Cast “) using the AE – Post Blocks Advanced widget.
Using Post Blocks Advanced, we will display the Cast details associated with that particular Movie.
Then we need to specify the name of the Relationship field type “related_cast.”
Here is the final look of the single post template designed for the post-type Movie.
Now, we will design the template for the post type Cast, which will include details of individual actors like names, images, and descriptions. Here, we will also be displaying all the movies in which the particular actor has worked. Since there is no direct relation between Cast and Movies, so, to display these details, we will be using the Reverse Relation feature of the Post Block Adv widget.
Create a new AE Template (AE Templates -> Add New) with the following configurations.
In Elementor editor, add the widgets like “AE – Post Title,” ‘AE – Content’, etc., to show the title, content, and other post data.
Now, we need to display all the movies that the particular Cast (Actor) has worked on. For this, we need to get data through a reverse query. Now, to display the data from Reverse Relation, we use the AE – Post Blocks Adv widget.
In widget settings, do the following configuration:
Then, all the Movies associated with the particular Actor are displayed. Here is the final look at the template that we have created.
To know more refer to the ACF Relationship Docs!
1. How To display ACF Reverse Relationship Using Post Blocks Adv?
2. How To Display ACF Relationship Fields data Using Post Blocks Adv?