Have a WordPress page with same title as custom post type “front” slug
Asked Answered
A

3

15

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?

Amaryllidaceous answered 25/7, 2015 at 0:49 Comment(3)
It's usually helpful to see some code. At the very least, the way you've defined (and tried to define) your rewrites.Ancalin
Where does Google show "children of pages"? I get that the subpages (here, single DJ posts) should have roughly the same permalink base for SEO, but it's my understanding that each page is ranked on its own merit, whether it's a Single Post of a Post Type is more a breadcrumbs issue. I usually handle this the same way you are now - /post-types/ is a Page with a nice overview, then /post-type/xyz/ is the specific details about the xyz post-type - never had SEO issues with it; both come up for post-type Google searchesPeepul
There's sometimes and odd bug in Wordpress when changing slugs, it will return a 404. Delete the pages with the same slugs, update the rewrite slug. Then goto settings > permalinks and resave the permalinks.Tobietobin
P
11

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- or parse_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:

! is_admin() && add_filter( 'request', function( $query_vars )  
{
    if ( isset( $query_vars['city'] ) && ! isset( $query_vars['djs'] ) )  
    {
        $slug = $query_vars['city'] . '-wedding-djs';

        // Override if the %city%-wedding-djs page exists
        if ( get_page_by_path( $slug ) )
            $query_vars['pagename'] = $slug;
    }
    return $query_vars;
} );

or if you only got boston and nyc as cities and you don't need to check if the corresponding pages exists:

! is_admin() && add_filter( 'request', function( $query_vars )  
{
    if ( isset( $query_vars['city'] ) && ! isset( $query_vars['djs'] ) )  
    {
        $city = $query_vars['city'];
        if ( in_array( $city, array( 'boston', 'nyc' ) ) )
            $query_vars['pagename'] = $city . '-wedding-djs';
    }
    return $query_vars;
} );

This means that the request:

example.tld/nyc-wedding-djs/

is now treated as a page, and

example.tld/nyc-wedding-djs/joe-smhoe/

as a djs post.

Here we assume that the page rewrite rules have lower priority than for the djs post type.

Providence answered 2/8, 2015 at 15:19 Comment(0)
L
7

If you are using htaccess you can manually set up the redirects for those pages and they should then resolve correctly. Add these lines :

RewriteCond %{REQUEST_URI} ^/boston-wedding-djs/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/boston-wedding-djs$ [NC]
RewriteRule . index\.php?pagename=boston-wedding-djs [NC,L]
RewriteCond %{REQUEST_URI} ^/nyc-wedding-djs/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/nyc-wedding-djs$ [NC]
RewriteRule . index\.php?pagename=nyc-wedding-djs [NC,L]

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.

Lookout answered 31/7, 2015 at 0:20 Comment(0)
S
7

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 a single-djs.php and that will display that page dynamically for you.

See this page for the template hierarchy

Shayshaya answered 31/7, 2015 at 0:39 Comment(1)
All three proposed solutions could solve the problem, but this is by far the least "hackish", cleanest, most future-proof solution - plus it's the only one that doesn't result in duplicate content as far as I can tell. It is always a good idea to consult the template hierarchy before doing workarounds - very often it provides a very simple solution to rather complicated problems.Algae

© 2022 - 2024 — McMap. All rights reserved.