I'm working on a TV website that has several custom post types that are separate, but related (e.g. Series, Episodes, Videos).
There are hierarchical relationships between these post types - Episodes belong to Series, and Videos can belong to Series and/or Episodes. These relationships are set through a meta key on the child entry which identifies the parent entry.
Structurally that seems to work OK, but I'm having trouble with the permalinks.
By default, WordPress displays the URLs as follows:
Series Archive and Single Entry (OK):
domain/series/
domain/series/series-name/
Individual Episode (Not OK):
/domain/episodes/episode-name/
With this permalink, isn't clear which Series the Episode is associated with. And in some cases where episodes have the same name (e.g. "Pilot" you end up with auto-generated names like "pilot-2" and "pilot-3".
Desired URL structure for individual episode (or video)
/domain/series/series-name/episodes/episode-name/
/domain/series/series-name/videos/video-name/
I tried to piece together some code for this based on some blog posts I found online, but it's not working. Here is the code I'm currently trying:
function custom_rewrites() {
global $wp_rewrite;
add_rewrite_tag('%episodename%', '([^&]+)', 'episodes=');
add_permastruct('episodes', '/series/series_placeholder/episodes/%episodename%', false);
add_rewrite_rule('^episodes/([^/]*)/([^/]*)/?','index.php?series=$matches[1]&episodes=$matches[2]','top');
}
add_action( 'init', 'custom_rewrites', 10, 0 );
I feel like I'm kind of close, but I'm clearly missing something (or several things).
Any help would be much appreciated!