Last active
July 2, 2023 16:18
-
-
Save kellenmace/fae42a47342d0ee4fe4a to your computer and use it in GitHub Desktop.
Remove custom post type slug from permalinks 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code works for Parent Pages but for the child pages.
I am trying to remove the word 'slug' here
domain.com/slug/hosting (works)
domain.com/slug/hosting/wp-engine (getting 404 error)
My CPT is hierachical where the second links is the child of the first link. I am getting 404 error for the second.