WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 7 weeks ago

#34136 new enhancement

Allow register_post_type's rewrite to remove CPT slug

Reported by: BenRacicot Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.3.1
Component: Rewrite Rules Keywords:
Focuses: Cc:

Description

Not sure if there are reasons (outside of naming conflicts) why register_post_type's rewrite does not allow an empty value as an argument to remove the post type slug from its future post URL's.

After a lot of research and some code from @JanBeck at my wordpress.stackexchange question: http://wordpress.stackexchange.com/questions/203951/remove-slug-from-custom-post-type-post-urls its clear that naming conflicts between a CPT and Page type URL's are being avoided.

I propose:

'rewrite' => array( 'slug' => '', 'with_front' => false ),
'has_archive' => 'something',  // will now default to false to avoid possible conflict

then proper rewrite rules should be added to allow for the removal of this CPT slug and disallow the CPT to create conflicting URLs in the future. eg publishing ones that match page URL's the same as native posts append a '-1' to a conflicting URL.

This will solve a large problem where users are trying to hack around this by creating their own rewrite rules, hacking post.php, and pre_get_posts solutions.

Change History (9)

#1 @moeloubani1
2 years ago

An easy way to do this would be to set a flag when registering the post type and if it is set then just add another rewrite rule that looks for the pagename and also checks it against that post type so that instead of 404 erroring after it checks to see if the page exists it checks one more time to see if it exists with post_type=nameofposttype and nameofposttype=url/that/was/requested

#2 @michael.ecklund
2 years ago

@BenRacicot Could you include a reason or reasons why this would be beneficial to the community?

#3 follow-up: @drzraf
2 years ago

Use-case: we have restaurants and had to tweak the rewrite (cf OP) in order to have provide URL like:

/<city>/<slug>

rather than the much more cumbersome:

/restaurant/<city>/<slug>

That best represents the main goal of the website which has no value being artificially namespaced.

We also have a couple of other CPT of less significance (and none of them is named after a city) so that conflict are not going to be an issue (or at least a minor issue in comparison to having clean URL)

Having an officially-supported way to omit the CPT URL's segment would be appreciated.

Last edited 2 years ago by drzraf (previous) (diff)

#4 in reply to: ↑ 3 ; follow-up: @michael.ecklund
2 years ago

I guess I don't understand why you wouldn't just use the built-in hierarchical Post Type (page) for that.

Replying to drzraf:

Use-case: we have restaurants and had to tweak the rewrite (cf OP) in order to have provide URL like:

/<city>/<slug>

rather than the much more cumbersome:

/restaurant/<city>/<slug>

That best represents the main goal of the website which has no value being artificially namespaced.

We also have a couple of other CPT of less significance (and none of them is named after a city) so that conflict are not going to be an issue (or at least a minor issue in comparison to having clean URL)

Having an officially-supported way to omit the CPT URL's segment would be appreciated.

#5 @BenRacicot
2 years ago

Hey @michael.ecklund,

Anytime you create a CPT for use as a method of public facing content creation in web page format where the use is meant to be browsable by URL. SEO, prett(ier) URLs etc...

The current functionality of CPTs should have the option to drop the CPT name from the URL path built in. Right now it's similar to as if native posts worked like this:

your.com/post/post-name

or if pages worked like:

your.com/page/page-name

But WP does not work this way because adding the CPT as a root path interferes with many SEO, promotional and content strategies. Please take a look at the [wordpress.stackexchange.com] question that has got a lot of attention. This functionality is desired by many.

https://wordpress.stackexchange.com/questions/203951/remove-slug-from-custom-post-type-post-urls?noredirect=1#comment397952_203951

Version 2, edited 2 years ago by BenRacicot (previous) (next) (diff)

#6 in reply to: ↑ 4 ; follow-up: @moeloubani1
2 years ago

Well as an example if I'm working on a learning website that has courses I'd rather have /course-title/chapter-name/class-name than /course/course-title/chapter-name/class-name and you're right, I could just put them into pages but imagine I have 300 classes and 10 pages, want to assign pages to some categories and courses to others etc. Now my category list is huge and the queries that happen for the workshop include the pages as well and you'd have to add some sort of other logic to differentiate between the two.

What adding a new post type does is gives you the organization to be able to show things in two areas, keeps things that aren't the same separate and helps when you're trying to query those things later, lets you add post type specific taxonomies, archives, etc etc. Sometimes you want all those benefits and you want the URLs to be a certain way and I think that CPTs should have the flexibility to support that.

Replying to michael.ecklund:

I guess I don't understand why you wouldn't just use the built-in hierarchical Post Type (page) for that.

Replying to drzraf:

Use-case: we have restaurants and had to tweak the rewrite (cf OP) in order to have provide URL like:

/<city>/<slug>

rather than the much more cumbersome:

/restaurant/<city>/<slug>

That best represents the main goal of the website which has no value being artificially namespaced.

We also have a couple of other CPT of less significance (and none of them is named after a city) so that conflict are not going to be an issue (or at least a minor issue in comparison to having clean URL)

Having an officially-supported way to omit the CPT URL's segment would be appreciated.

#7 in reply to: ↑ 6 @BenRacicot
2 years ago

What if you're using pages as they are meant to be used? As the standard web page layout... and your CPT's are being used as a custom web page layout or content as I described:

Anytime you create a CPT for use as a method of public facing content creation in web page format where the use is meant to be browsable by URL. SEO, prett(ier) URLs etc...

Replying to moeloubani1:

Well as an example if I'm working on a learning website that has courses I'd rather have /course-title/chapter-name/class-name than /course/course-title/chapter-name/class-name and you're right, I could just put them into pages but imagine I have 300 classes and 10 pages, want to assign pages to some categories and courses to others etc. Now my category list is huge and the queries that happen for the workshop include the pages as well and you'd have to add some sort of other logic to differentiate between the two.

What adding a new post type does is gives you the organization to be able to show things in two areas, keeps things that aren't the same separate and helps when you're trying to query those things later, lets you add post type specific taxonomies, archives, etc etc. Sometimes you want all those benefits and you want the URLs to be a certain way and I think that CPTs should have the flexibility to support that.

Replying to michael.ecklund:

I guess I don't understand why you wouldn't just use the built-in hierarchical Post Type (page) for that.

Replying to drzraf:

Use-case: we have restaurants and had to tweak the rewrite (cf OP) in order to have provide URL like:

/<city>/<slug>

rather than the much more cumbersome:

/restaurant/<city>/<slug>

That best represents the main goal of the website which has no value being artificially namespaced.

We also have a couple of other CPT of less significance (and none of them is named after a city) so that conflict are not going to be an issue (or at least a minor issue in comparison to having clean URL)

Having an officially-supported way to omit the CPT URL's segment would be appreciated.

#8 @BenRacicot
9 months ago

The stack question keeps getting votes. Is there anyone who knows how to bump an admin who works on the core trac? Perhaps @GaryPendergast ?

#9 @freshyjon
7 weeks ago

I agree that this needs to be implemented, as a way to remove the Custom Post Type slug from the permalink. It helps for organization where post types are used for different "types" of content/layouts/templates... while still allowing for the ability to have a clean URL, which also checks against existing Page slugs (to ensure no duplicates).

Note: See TracTickets for help on using tickets.