How to Remove the Custom Post Type Slug from the Permalink(URL) Structure in WordPress

Recently we were hired to redesign a website with great SEO stats that had a very unique URL structure set up. Since their SEO stats were very good we were challenged with the task of rebuilding this website in WordPress to allow for easier dashboard maintenance while keeping the same URLs in order to not lose any valuable SEO. While certain URLs were fairly easy to duplicate it was the custom post type permalink structure that was rather more complicated than anticipated to maintain. We needed to keep the custom post types set up so that maintenance was easy in the backend but we somehow needed to remove the custom post type slug from the permalink(url) structure. Sounds easy but as we tried several attempts without any luck we quickly realized that this is not as simple as we thought. Luckily persistence paid off because we finally found a method that accomplished everything we were trying to achieve. In this tutorial we will teach you how to remove the custom post type slug from the permalink(url) structure in WordPress without using a plugin.

Remove the Custom Post Type Slug from the Permalink(URL) Structure using WordPress PHP Functions

In order to remove the custom post type slug from the permalink structure you will need to add the following PHP to the WordPress functions file. Simply open your functions.php file, copy and paste the code below then follow the steps below to edit for your specific custom post type.

PHP

 function dd_remove_posttype_slug( $post_link, $post, $leavename ) {
     if ( ! in_array( $post->post_type, array( 'your-post-type' ) ) || 'publish' != $post->post_status )
         return $post_link;
     $post_link = str_replace( '/your-post-type-slug/', '/', $post_link );
         return $post_link;
 }
 add_filter( 'post_type_link', 'dd_remove_posttype_slug', 10, 3 );
 function dd_parse_request_posttype( $query ) {
     if ( ! $query->is_main_query() )
         return;
     if ( 2 != count( $query->query )
         || ! isset( $query->query['page'] ) )
         return;
     if ( ! empty( $query->query['name'] ) )
         $query->set( 'post_type', array( 'post', 'page', 'your-post-type' ) );
 }
 add_action( 'pre_get_posts', 'dd_parse_request_posttype' ); 
  1. Replace ‘your-post-type’ on line 2 and line 15 with your actual custom post type.
  2. Replace ‘your-post-type-slug’ on line 4 with the slug for your actual custom post type.
  3. Upload your functions.php file to your server.
  4. Refresh your permalink settings in the WordPress dashboard.

How to Remove the Slug from the Permalink Structure of Multiple Custom Post Types using WordPress PHP Functions

In order to remove the slug from the permalink structure of multiple custom post types you will use the same code above but with slight modifications. Simply open your functions.php file, then copy and paste the code below and follow the steps below to edit for your specific custom post types.

PHP

 function dd_remove_posttype_slug( $post_link, $post, $leavename ) {
     if ( ! in_array( $post->post_type, array( 'your-post-type1', 'your-post-type2' ) ) || 'publish' != $post->post_status )
         return $post_link;
     $post_link = str_replace( '/your-post-type-slug1/', '/', $post_link );
     $post_link = str_replace( '/your-post-type-slug2/', '/', $post_link );
     return $post_link;
 }
 add_filter( 'post_type_link', 'dd_remove_posttype_slug', 10, 3 );
 function dd_parse_request_posttype( $query ) {
     if ( ! $query->is_main_query() )
         return;
     if ( 2 != count( $query->query )
         || ! isset( $query->query['page'] ) )
         return;
     if ( ! empty( $query->query['name'] ) )
         $query->set( 'post_type', array( 'post', 'page', 'your-post-type1', ’your-post-type2' ) );
 }
 add_action( 'pre_get_posts', 'dd_parse_request_posttype' );  
  1. Replace ‘your-post-type1’ and ‘your-post-type2’ on line 2 and line 16 with your actual custom post types.
  2. Replace ‘your-post-type-slug1’ on line 4 and ‘your-post-type-slug2’ on line 5 with the slugs for your actual custom post types.
  3. Upload your functions.php file to your server.
  4. Refresh your permalink settings in the WordPress dashboard.

Troubleshooting

If you are having problems getting this tutorial to work please reread the tutorial and try again. Please do not email us with problems regarding this tutorial unless you want to hire us.