Skip to content

Instantly share code, notes, and snippets.

@brianleejackson
Last active June 1, 2023 11:41
Show Gist options
  • Save brianleejackson/4ef9d7f8a65089e4ee903de030dd0b2b to your computer and use it in GitHub Desktop.
Save brianleejackson/4ef9d7f8a65089e4ee903de030dd0b2b to your computer and use it in GitHub Desktop.
Remove base slug from custom post type URL. Source: https://woorkup.com/wordpress-custom-post-type/
function na_remove_slug( $post_link, $post, $leavename ) {
if ( 'artist' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );
function na_parse_request( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'artist', 'page' ) );
}
}
add_action( 'pre_get_posts', 'na_parse_request' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment