Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/65d100fa6c76d249c53f to your computer and use it in GitHub Desktop.
Save kellenmace/65d100fa6c76d249c53f to your computer and use it in GitHub Desktop.
Remove Custom Post Type Slug from Permalinks 2
/**
* Have WordPress match postname to any of our public post types (page, post, race)
* All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
* By default, core only accounts for posts and pages where the slug is /post-name/
*/
function gp_parse_request_trick( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'race', 'page' ) );
}
}
add_action( 'pre_get_posts', 'gp_parse_request_trick' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment