1

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!

1 Answer 1

0

I think it is better to connect with taxonomy terms. Since you can add any custom fields to terms using ACF / another plugin / code, it is possible to use terms with images, full description etc.

I had similar task, this tutorial should be helpful https://rudrastyh.com/wordpress/taxonomy-slug-in-post-type-url.html. Yes, I know that there is no hierarchy but I want to show you another principle to implement your task.

Actually to add an hierarchy you just have to reorder terms from this part of the code:

$terms = wp_get_object_terms( $post->ID, $tax_name );

And add them in this part:

$permalink = str_replace( $post_type_slug, $terms[0]->slug . '/' . $terms[0]->slug, $permalink );

If you have a specific quetion - ask in comments - here or there as you want. I will try to help you.

2
  • Hi Misha, thanks for the response! So are you suggesting that "Series" would be better as a Taxonomy with custom fields, and then Episodes and Videos are assigned to that taxonomy? The individual Series entries don't have much of their own "body" content right now, so that might work. In terms of the URLs, though we would still want them to be /series/episodes/episode-name or /series/videos/video-name/ . Is that what the code you posted is intended to do? Oct 19, 2017 at 18:34
  • Hi, yes, you're right. That code is about it. Oct 19, 2017 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.