Show related posts from one CPT to another with ACF Relationships
Showing related posts from one Custom Post Type to another is possible with an ACF Relationship field.
This tutorial uses 2 CPTs, ‘Lights’ and ‘Projects’, the intent is to show the ‘Lights’ used in each ‘Project’ so the reader can click the link back to see the light from a project CPT. (This is also easily done using the default ‘Post’ type with another CPT.
First up is to create a ACF Relationship field for the ‘Lights’ CPT and display it on the ‘Projects’ CPT post admin backend.
So above a field name is given and the ‘Relationship’ field type is selected, The Lights CPT is selected as the filter in ‘Filter by Post Type’ and also a custom taxonomy used in the Lights CPT is also selected, this will allow the user to filter posts by taxonomy.
This field group is then assigned to appear on the Projects CPT.
Relationship Field in Back End of Custom Post type
Now in the Projects CPT post the relationship field is visible, the posts can be search for or filtered by taxonomy, then when selected move from the left to right dialog boxes to indicate that they are the related posts.
Displaying the Related Posts
The code used to output the related posts can be based on the ACF code and enhanced from there. You can add with PHP direct in the relevant template or placed via a shortcode.
In the above snippet I am creating a shortcode [tl_related_lights] that will output a list of post titles with clickable links of ‘Light’ CPTs in each Project ‘CPT’.
A list of clickable lights for each project.
Bi-directional Related Posts
It is possible to link posts bi-directionally with the use of an ACF plugin ACF Post-2-Post available also on WordPress plugin repo. To enable the functionality between the 2 CPTs you set up the relationship field on the other CPT using the same field name – in this case ‘tl_project_lights_link_used’ – also you have to add the ACF code on the other CPT template.