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!

? Note: We are not able to help with custom coding, nor can we provide troubleshooting help for customizing the code we are providing to you. These facts are elaborated upon here and here. This guide is simply a starting place for learning about some of the advanced features of our plugin code.

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

Screenshots

Here are some visuals of the functionality mentioned above:

Creating an Instructor linked post type post from the Event edit screen
The Instructors Post Type Added to Events
The Instructor post type successfully added to Events, with the same capabilities as the default Organizers and Venues types.
Single Events page: Instructors are displayed in the meta box below Details and Organizer

Related Resources

The 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):

<?php

/**
 * The Events Calendar: Remove the Organizers post type from Events.
 *
 * Replace instances of ORGANIZER_POST_TYPE with VENUE_POST_TYPE if you
 * want to do so for Venues instead.
 *
 * @link https://theeventscalendar.com/knowledgebase/linked-post-types/
 * @link https://gist.github.com/a521d02facbc64ce3891c9341384cc07
 */
function tribe_remove_organizers_from_events( $default_types ) {

	if (
		! is_array( $default_types )
		|| empty( $default_types )
		|| empty( Tribe__Events__Main::ORGANIZER_POST_TYPE )
	) {
		return $default_types;
	}

	if ( ( $key = array_search( Tribe__Events__Main::ORGANIZER_POST_TYPE, $default_types ) ) !== false ) {
		unset( $default_types[ $key ] );
	}

	return $default_types;
}

add_filter( 'tribe_events_register_default_linked_post_types', 'tribe_remove_organizers_from_events' );
@thenomadicmann

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:

The Organizers Type Removed from Events
The default Organizers post type successfully removed from Events, while the custom Instructors post type remains.

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:

/wp-content/plugins/the-events-calendar/src/functions/advanced-functions/linked-posts.php

All of the available functions are as follows:

  • tribe_register_linked_post_type
  • tribe_deregister_linked_post_type
  • tribe_has_linked_posts
  • tribe_get_linked_posts
  • tribe_has_linked_posts_by_post_type
  • tribe_get_linked_posts_by_post_type
  • tribe_get_linked_post_types
  • tribe_has_linked_post_types
  • tribe_is_linked_post_type
  • tribe_link_post
  • tribe_unlink_post
  • tribe_get_linked_post_container
  • tribe_get_linked_post_id_field_index
  • tribe_get_linked_post_name_field_index

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!