Removing the Base or Slug from a Custom Post Type URL in WordPress [Method Breaks RSS Feeds and Pages]

Remove the slug or post type base from your custom post type URLs.

Baseless Permalink

UPDATE: Something I didn’t foresee was this method of removing custom post type slugs from WordPress would break RSS feeds.  I’m trying to find an alternative route to removing the post type slug from the URL and I’ll update with a new post when that happens.  In the meantime, if you need RSS feeds working I wouldn’t use this method.  If RSS feeds aren’t important to you, have at it.

I’m in the process of creating a new plugin that will allow WordPress users to group posts into series. Sure, there are a few plugins out there that does this already, but I have some ideas that are a bit different and am hoping to improve the UI/UX of plugins of that sort. Anyway, one of the things I was trying to achieve at least for this site was to remove the Base or Slug from the series post URL.  In other words “http://bigblackguy.com/this-is-a-series” instead of “http://bigblackguy.com/slug/this-is-a-series”.

It’s something I tried before on MASH while I was redoing that site, but it really never worked out and I didn’t push the issue because I was already a month behind my release schedule. Turns out I was pretty close to actually doing it, I just missed one key step which I’ll mention below.  First lets take a look at the code you should have in place or may need to put in place.

add_action('init','customPostTypes);
function customPostTypes(){
     register_post_type('slug', array(
         'label' => __('slug'),
         'singular_label' => __('slug'),
         'public' => true,
         'publicly_queryable' => true,
         'show_ui' => true,
         'show_in_menu' => true,
         'menu_position' => 5,
         'capability_type' => 'page',
         'supports' => array('title','editor','thumbnail','excerpt','comments','revisions'),
         'query_var' => 'slug',
         'taxonomies' => array('post_tag'),
         'rewrite' => array('slug' => '/','with_front' => true)
     ));
}

The above code is just an example and doesn’t need to look exactly like the code in your theme (or plugin). Most of my custom post types are based on the current theme, so I usually have these in my functions.php. I’m probably going to cover the register_post_type() method at some other time, but for now you can get more info on the method in the WordPress codex. The important part that we want to zero in on is where it says rewrite.

For the longest time I would either set rewrite to true and allow the defaults to override, or set the slug to whatever the label was for the post type. To achieve my goal I needed to set the slug to “/” and set the “with_front” to true. That is something I tried when building MASH 2.0, but I missed one critical step — I needed to flush the rewrite rules. There is a function that does this for you, but should only be used when absolutely necessary; it’s a bad idea to put it in your functions.php and leave it there due to how extensive the process is.  However, putting it in my functions.php to execute once and take it back out feels a bit cumbersome. Luckily there is a much easier way for you to flush your rewrite rules.

It’s as simple as going to your permalink settings and saving. That’s it. You don’t even need to make any changes. After I added added those parameters to rewrite and flushed the rewrite rules via permalink settings, the base was removed and you can see the results in the image above. You may ask why I don’t just use pages to accomplish what was looking to do, and at first I did. After looking at the information for the hierarchical parameter (which essentially turns your post type into a page type) I found this tidbit of information:

Note: this parameter was planned for Pages. Be careful, when choosing it for your custom post type – if you are planning to have many entries (say – over 100), you will run into memory issue. With this parameter set to true WordPress will fetch all entries of that particular post type, together with all meta data, on each administration page load for your post type.

It looks like WordPress designed pages to be used sparingly, and not saying that I will be setting up over 100 series, but I don’t want to build something that already has a limit in mind.  Anyway, here is a quick recap of how to remove the Slug from your post URLs in WordPress.

  1. Find or add your code with register_post_type.
  2. Add or modify the rewrite parameter with an array with the ‘slug’ => ‘/’ and ‘with_front’ => true parameters.
  3. Go to your permalink settings (WordPress Admin > Settings > Permalink) and press “Save”.
'rewrite' => array('slug' => '/','with_front' => true)