Displaying Relationships on the Frontend
Explore how to retrieve and display relationships between custom post types on a WordPress site using the Advanced Custom Fields plugin. Understand how to safely fetch, loop through, and format related posts like courses on event and teacher pages. Discover techniques to avoid errors by conditionally checking data and learn how to use setup_postdata and wp_reset_postdata functions for managing global post objects during display.
We'll cover the following...
The ACF plugin gives us access to the get_field() function to get the value of the custom field. This function returns an array of post objects.
We will create a variable $relatedCourses to store the value of the custom field. This variable stores the array of Course objects.
Display related courses on event post
Open the single-event.php. We will add the code to display the related courses after the event date, time and location information. Using the get_field_ function, fetch the value of the related_courses custom field as follows:
<?php$relatedCourses = get_field('related_courses');?>
To find the name of the custom field, go to the admin dashboard. From the sidebar choose, "ACF." Then click the desired field group. The name of the field is ...