Se você quiser manter 'autores' como o slug base nos permalinks, ou seja, example.com/authors/stephen-king / para o CPT dos autores, example.com / autores / stephen-king / the-shining / para o CPT de 'livros' e example.com/authors/stephen-king/the-shining/chapter-3 / para o ' CPT dos capítulos, o WordPress vai pensar que praticamente tudo é um post de 'autores' ou um filho hierárquico de um post de 'autores' e, como esse não é o caso, o WordPress acaba se tornando muito confuso.
Com isso dito, há uma solução que é bem básica, mas desde que a estrutura do link permanente sempre siga a mesma ordem, ou seja, a palavra 'autores' é sempre seguida por um slug de autor, que é sempre seguido por um slug de livro é sempre seguido por uma lesma de capítulo, então você deve ser bom para ir.
Nesta solução, não há necessidade de definir o slr de reescrita na definição de tipo de postagem personalizada para 'capítulos' e 'livros', mas defina o caractere de reescrita dos 'autores' como simplesmente 'autores', coloque o seguinte código em seu arquivo functions.php e "flush" suas regras de reescrita.
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;
}