Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 857
HandiworkNYC.com Avatar asked Jul 25 '15 00:07

HandiworkNYC.com


People also ask

How do I display custom post in front page?

Go to the static front page section in the customizer, set the front page to show the latest posts, and preview the front page to show the front page post type option.

How do I display custom post type in front end in WordPress?

All you need to do here, is create a file called archive-podcasts. php in your theme's directory. If such file exists, WordPress will automatically use this template file when displaying content from your custom post type.

How do I change the slug of a post in WordPress?

Changing tag slugs is similar. Go to Posts and choose Tags. Find the tags you want to edit and click the Edit button. After you input your tag slug, click Update to save the change.


3 Answers

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.

like image 124
birgire Avatar answered Oct 19 '22 00:10

birgire


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.

like image 23
trex005 Avatar answered Oct 18 '22 22:10

trex005


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

like image 7
weston deboer Avatar answered Oct 19 '22 00:10

weston deboer