Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove custom post type slug from permalinks 2
<?php
/**
* Have WordPress match postname to any of our public post types (post, page, race).
* All of our public post types can have /post-name/ as the slug, so they need to be unique across all posts.
* By default, WordPress only accounts for posts and pages where the slug is /post-name/.
*
* @param $query The current query.
*/
function gp_add_cpt_post_names_to_main_query( $query ) {
// Bail if this is not the main query.
if ( ! $query->is_main_query() ) {
return;
}
// Bail if this query doesn't match our very specific rewrite rule.
if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
return;
}
// Bail if we're not querying based on the post name.
if ( empty( $query->query['name'] ) ) {
return;
}
// Add CPT to the list of post types WP will include when it queries based on the post name.
$query->set( 'post_type', array( 'post', 'page', 'race' ) );
}
add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );
@komapeb

This comment has been minimized.

Copy link

komapeb commented May 3, 2018

Is there a way to make this work with hierarchical CPT?
E.g.
example.com/parent-1/subpage
example.com/parent-2/subpage

Subpage has the same name, with a different parent, and has different contents. Just the slugs are identical.

@Garconis

This comment has been minimized.

Copy link

Garconis commented Jul 3, 2019

@kellenmace is this still working? For some reason, this still allows me to create a slug on my CPT that already exists on a live Page.

@kellenmace

This comment has been minimized.

Copy link
Owner Author

kellenmace commented Jul 5, 2019

Hey @Garconis - Yeah, this still works and has for several years. I would make sure you’re using both code snippets from my blog post, though: https://kellenmace.com/remove-custom-post-type-slug-from-permalinks/ If you are missing the other one, that may be why WP is allowing your custom post type and pages to have the same slugs.

@TailonR

This comment has been minimized.

Copy link

TailonR commented May 22, 2020

Your blog post said to create a plugin, do I insert both snippets into one php file? or two?

@kellenmace

This comment has been minimized.

Copy link
Owner Author

kellenmace commented May 25, 2020

@TailonR Both snippets can go in the same PHP file. So the plugin will end up looking something like this: https://gist.github.com/kellenmace/f8a3393385f01ee226a087ff19cc6056

@TailonR

This comment has been minimized.

Copy link

TailonR commented May 26, 2020

@kellenmace Thank you once I did that, I change "race" on lines 16 and 49, and for consistency, on line 25, and it worked.

@kellenmace

This comment has been minimized.

Copy link
Owner Author

kellenmace commented Jun 3, 2020

@TailonR Nice! Glad to hear it. 👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.