While The Events Calendar provides the Organizer and Venue post types by default, you may want to have another post type to associate with events, like “Speakers” or “Instructors”. Like Organizers, you would be able to add existing Instructors to events, or create new Instructors while creating events.
Doing this before version 4.2 of The Events Calendar meant lots of extra custom coding, but since 4.2 you can add or remove event-related post types with just a few simple filters. This framework for relating post types to events is called the “Linked Post Types” system.
Although the code added in 4.2 (and some deduplication functionality added in version 4.6) makes Linked Post Types customizable in a lot of neat ways, it actually still takes a heavy amount of code to actually add a new custom post type, link it to events, add new custom fields and validate their input, display these on the Single Events page, and display upcoming events on their single pages.
If you visited this guide before 2018, it pointed you down the right path but not enough to actually have you hit the ground running. Now, we are providing you with a fully-working example plugin!
Example “Instructor” Linked Post Type plugin
The download link for this boilerplate/starter plugin is at its extensions page: https://theeventscalendar.com/extensions/linked-post-type-instructors/
You may also choose to review its code or submit pull requests to its GitHub repository.
We always strongly recommend you make backups of your site and database and try new things (adding a plugin, updating a plugin, customizing a plugin, etc.) on a staging site before doing so on your live site!
This plugin is intended to be forked! “Forking” code means copying it and then modifying it. It’s a starting point, a boilerplate. It uses our Extensions Framework to do a few helpful things like ensure we have a compatible version of The Events Calendar and that our code loads after it so we can use its functions.
However, we determined this plugin is not suitable for inclusion in our Extensions Library because it’s not intended to be plug-and-play. This plugin is for people comfortable with tweaking or writing custom code.
After all that, if you decide you would like to know more about Linked Post Types, here are some highlights of what this plugin does:
- Creates a new Instructor custom post type with applicable capabilities for various user types (administrator, editor, etc.)
- Flushes Permalinks whenever visiting the wp-admin Plugins List
- Adds Phone, Website, and Email Address custom fields and custom meta box
- Allows for sanitizing the input of these custom fields—e.g. email address gets ran through sanitize_email()
- Builds the custom field keys (what gets saved to the database) from the custom field labels (what the user sees)
- Saves the required custom field to link Instructors to Events
- Outputs the linked Instructors (and each’s custom fields) on the Single Events page, inside the same metabox as Details and Organizers
- Tells WP_Query that querying for an Instructor should count as an “event query”, without which The Events Calendar wouldn’t know to do some things it has to do for all this to work
- Loads the included single.php for the Single Instructor pages, which follows our Themer’s Guide’s template override system (NOTE the single.php’s parent folder needs to match the custom post type’s key)
- Displays Upcoming Events on the Single Instructor page
- Adds a “There were no results found.” notice on the Single Instructor page if there are no events found
Here are some visuals of the functionality mentioned above:
Following are some tips for how to do things the provided starter plugin does not do for you.
Removing Default Post Types from Events
If you want to remove a default post type from events—either Organizers or Venues—you would add a snippet like this somewhere in your custom code (such as a separate custom plugin):
Here is a screenshot showing the section of the edit-event screen with our previously-added Instructors post type available, but with the default Organizers post type removed:
Advanced Functions and Learning More
There is an array of functions related to Linked Post Types, all of which can be found in this file within The Events Calendar:
All of the available functions are as follows:
You may choose to leverage one of these functions on top of using the Instructors Linked Post Type nearly as-is, or you may choose to significantly modify this plugin. Either way, we hope this gets you up-and-running quickly with all your custom linked post needs!