The way I see it, you could do:
// Via the init hook, do all these:
register_post_type( 'library-books', [
'public' => true,
'label' => 'Library Books',
'rewrite' => [ 'with_front' => false, 'slug' => 'library/books' ],
'has_archive' => 'library/books',
// other args here
] );
register_post_type( 'library', [
'public' => true,
'label' => 'Libraries',
'rewrite' => [ 'with_front' => false, 'slug' => 'library' ],
'has_archive' => false,
// other args here
] );
add_rewrite_rule( '^library/?$', 'index.php?library=home', 'top' );
I.e.
-
Register the
library-books
CPT first, withlibrary/books
as the rewrite slug as well ashas_archive
. -
Then register the
library
CPT withlibrary
as the rewrite slug, andhas_archive
set tofalse
. -
And register a custom rewrite rule for the
example.com/library
which (internally) loadsexample.com/library/home
(which is a post of thelibrary
CPT, although you can really modify the rewrite rule’s query to load a post of a different post type such aspage
).
But of course, in your library
CPT, you shouldn’t have a post with books
as the slug.