I have a custom post type djs
and a custom taxonomy city
with two terms: boston
and nyc
. So a DJ can either be tagged as Boston or NYC.
DJ profiles (a single djs
post) are located at /nyc-wedding-dj/joe-shmoe/
, for example, or for a Boston DJ, /boston-wedding-dj/jane-doe/
. In other words, the rewrite slug for that custom post type is set as %city%-wedding-dj
.
I have two regular pages called /boston-wedding-djs/
and /nyc-wedding-djs/
(note the plural DJs with an s) where I use a custom page template to loop through the DJs from the corresponding city and display a list of links to DJ Profiles from that page’s city.
What I would like to do is to have the rewrite slug be %city%-wedding-djs
so that the custom post type single pages “appear” (at least to google) as child pages of those pages mentioned above.
There’s a conflict however, that when I change the rewrite slug for the custom post type djs
to %city%-wedding-djs
, I get a 404 Not Found error for the pages /boston-wedding-djs/
and /nyc-wedding-djs/
.
How can I have a rewrite slug with the same name as a page?
This reminded me of a great blog post by Matthew Boynes on a somewhat related subject.
It’s recommended to use a unique base slug, to avoid the clashes you’re describing.
If you must do this and are looking for a workaround within WordPress, then it’s possible to adjust how it parses the incoming request, with the help of the
request
– orparse_request
filter.Example:
This checks if the
%city%-wedding-djs
page exists, so you don’t need to update the code snippet if you later add more cities:or if you only got
boston
andnyc
as cities and you don’t need to check if the corresponding pages exists:This means that the request:
is now treated as a
page
, andas a
djs
post.Here we assume that the
page
rewrite rules have lower priority than for thedjs
post type.If you are using htaccess you can manually set up the redirects for those pages and they should then resolve correctly. Add these lines :
after
RewriteRule ^index.php$ - [L]
You may also be able to do this with WordPress add_rewrite_rule however it already seems like they are confused so it seems that is less likely to work.
You can’t have a post and a page with the same slug.
You need to remove your page and create a template page of
archive-djs.php
and maybe asingle-djs.php
and that will display that page dynamically for you.See this page for the template hierarchy