url rewriting – Share root slug for Child Page and Custom Post Type (prioritizing child pages over posts)


Let’s suppose a Custom Post Type called “Whatever”, which will have a root slug set to “whatever”.

This makes all posts of type “whatever” be available at site.url/whatever/post-name

I also want a parent page called Whatever to be available at site.url/whatever

To achieve that, I must create the page with said slug and also set the has_archive property to false, when registering the “Whatever” CPT.

So far so good.

Now, I want to have some specific child pages for the page “Whatever”, and I want them to be available at site.url/whatever/child-page-name

This last step is not working because when accessing that url, WordPress tries to load a CPT of type “Whatever” with name “child-page-name”. And it does not exist.

What I want to do is to prioritize pages during the loading process, so that if a child page of “Whatever” exists with the requested name, it should always be loaded (even if a CPT exists with said name).

I don’t know where to start. I thought about having the single-whatever.php file look for a page with the requested post name. But, I don’t know what to do with that information if the page exists. Should I redirect? Filter the loaded template? Here is where I am stuck, and any help will be appreciated.

Alvaro Franz 2 months 2021-10-14T09:28:42-05:00 0 Answers 0 views 0

Leave an answer