Wordpress - Nested custom post types with permalinks
If you want to keep 'authors' as the base slug in the permalinks, i.e. example.com/authors/stephen-king/ for the 'authors' CPT, example.com/authors/stephen-king/the-shining/ for the 'books' CPT and example.com/authors/stephen-king/the-shining/chapter-3/ for the 'chapters' CPT, WordPress will think pretty much everything is an 'authors' post or a hierarchical child of an 'authors' post and, since that is not the case, WordPress ultimately becomes very confused.
With that said, there's a workaround that is quite basic but as long as your permalink structure always follows the same order, i.e. the word 'authors' is always followed by an author slug, which is always followed by a book slug which is always followed by a chapter slug, then you should be good to go.
In this solution, there's no need to define the rewrite slug in the custom post type definition for 'chapters' and 'books', but set the 'authors' rewrite slug as simply 'authors', place the following code in your functions.php file and "flush" your rewrite rules.
add_action( 'init', 'my_website_add_rewrite_tag' );
function my_website_add_rewrite_tag() {
// defines the rewrite structure for 'chapters', needs to go first because the structure is longer
// says that if the URL matches this rule, then it should display the 'chapters' post whose post name matches the last slug set
add_rewrite_rule( '^authors/([^/]*)/([^/]*)/([^/]*)/?','index.php?chapters=$matches[3]','top' );
// defines the rewrite structure for 'books'
// says that if the URL matches this rule, then it should display the 'books' post whose post name matches the last slug set
add_rewrite_rule( '^authors/([^/]*)/([^/]*)/?','index.php?books=$matches[2]','top' );
}
// this filter runs whenever WordPress requests a post permalink, i.e. get_permalink(), etc.
// we will return our custom permalink for 'books' and 'chapters'. 'authors' is already good to go since we defined its rewrite slug in the CPT definition.
add_filter( 'post_type_link', 'my_website_filter_post_type_link', 1, 4 );
function my_website_filter_post_type_link( $post_link, $post, $leavename, $sample ) {
switch( $post->post_type ) {
case 'books':
// I spoke with Dalton and he is using the CPT-onomies plugin to relate his custom post types so for this example, we are retrieving CPT-onomy information. this code can obviously be tweaked with whatever it takes to retrieve the desired information.
// we need to find the author the book belongs to. using array_shift() makes sure only one author is allowed
if ( $author = array_shift( wp_get_object_terms( $post->ID, 'authors' ) ) ) {
if ( isset( $author->slug ) ) {
// create the new permalink
$post_link = home_url( user_trailingslashit( 'authors/' . $author->slug . '/' . $post->post_name ) );
}
}
break;
case 'chapters':
// I spoke with Dalton and he is using the CPT-onomies plugin to relate his custom post types so for this example, we are retrieving CPT-onomy information. this code can obviously be tweaked with whatever it takes to retrieve the desired information.
// we need to find the book it belongs to. using array_shift() makes sure only one book is allowed
if ( $book = array_shift( wp_get_object_terms( $post->ID, 'books' ) ) ) {
// now to find the author the book belongs to. using array_shift() makes sure only one author is allowed
$author = array_shift( wp_get_object_terms( $book->term_id, 'authors' ) );
if ( isset( $book->slug ) && $author && isset( $author->slug ) ) {
// create the new permalink
$post_link = home_url( user_trailingslashit( 'authors/' . $author->slug . '/' . $book->slug . '/' . $post->post_name ) );
}
}
break;
}
return $post_link;
}
Learn more about the CPT-onomies plugin
I don't have personal experience with such a scenario, but Randy Hoyt did a presentation at WordCamp San Fran last weekend about "Subordinate Post Types" that sounds like what you're talking about.
Here's his page for the talk that includes his presentation slides and links to a plugin he built for working with subordinate post types: http://randyhoyt.com/wordpress/subordinate-post-types/