Posted in: WordPress Tutorials

How to Remove Custom Post Type Slug from Permalink in WordPress?

Don’t want the custom post type URL to include the post type in the URL? Here’s how to remove custom post type slug from URL in WordPress.

Custom post types can be added in WordPress using the register_post_type function. When you need to have a post type other than posts and pages, it’s a great way to implement. With custom post types, your content is organized in a better manner and you can also handle special functions, post meta and design better.

What if you need to register a custom post type, but don’t want to have the extra slug in the custom post type permalink?

For instance, if you register a custom post type such as “services”, your custom post URL includes “services” or your defined slug. This makes the URL longer, and might not be required.

You might want your URL to be like example.com/web-design/ and not example.com/services/web-design/

Here’s how a custom post type is registered in WordPress.

register_post_type( string $post_type, array|string $args = array() )

Here are the parameter that are supported with the function.

  • $post_type: (string) (Required) Post type key (name)
  • $args: (array|string) (Optional) Array or string of arguments for registering a post type.

The $args parameter is an array for granular control of the custom post type. You can check the detailed the arguments accepted in the $args parameter on WordPress.org.

Among the $args, one can define whether the custom post type has archives and also control how the URL permalinks for the custom post type are constructed.

  • ‘has_archive’ : (bool|string) Whether there should be post type archives, or the archive slug to use. Will generate the proper rewrite rules if $rewrite is enabled. Default false.
  • ‘rewrite’: (bool|array) Handling rewrites for this post type. Defaults to true, using $post_type as slug. An array can be passed instead of just true / false to specify rewrite rules.

The “rewrite” parameter of $args supports the following keys.

  • ‘slug’: (string) The permalink structure slug. Defaults to $post_type key.
  • ‘with_front’: (bool) Whether the permalink structure should be prepended with WP_Rewrite::$front. Default true.
  • ‘feeds’: (bool) Whether the feed should be enabled.
  • ‘pages’: (bool) Pagination enabled or disabled. Default true.
  • ‘ep_mask’: (const) Endpoint mask to assign.

The “slug” parameter is where the permalink structure for the custom post type URL is handled.

By default, it prepends the custom post slug with the custom post type key (name).

If the custom post type is “services”, the default URLs include it. Eg: example.com/services/web-design

One can define what slug to use by setting the same here.

In order to use our desired custom post type slug in the URL, we can change the same to any string (alphabet, numeric and underscores) that we want.

Our objective is to remove the custom post type slug from the URL. Leaving the slug value as blank doesn’t remove the slug. Instead, it makes WordPress to use the default.

You can force WordPress to remove the custom post type slug from permalink structure by setting the slug to “/”. That’s all you need to do.

Here’s how your custom post type code might look.

// Register services custom post type
register_post_type( 'services', $args );
$args = array( 'has_archive' => true, 'rewrite' => $rewrite);
$rewrite = array('slug' => '/', 'with_front' => true);

Once you make the change to your custom post type code, you need to save your permalink settings. Simply log in to your WordPress dashboard, go to the Settings -> Permalinks page and hit the save button. This will save the permalink settings and the .htaccess file will be updated accordingly.

Don’t forget to delete your website cache to see the change on your website.

WordPress will easily handle your old URLs and redirect them to the new ones. However, when you change the permalink of your website, you should add permanent redirections for the old URLs for better SEO.