WordPress has quite a few options to re-write the permalink structure for Blog posts and Custom post type. In this post I will focus on both the posts and custom posts. However, this post would remain incomplete for quite sometime and I would update it every now and then (whenever, I find something new).
Every time you change the permalink structure of yours post (from Settings > Permalinks) or custom post type (using functions), you should always go back to default settings first. You can easily do that by simply following these steps:
Step 1 : Go to the “Settings” option from the admin panel and click on Permalinks.
Step 2 : From “Common Settings” option, select the “Default” radio button.
Step 3 : From the “Optional” section at the bottom, delete anything that you have on the base (category, tags) input fields (write it down somewhere, as you would need it later on) and hit “Save Changes” button.
Step 4 : Open up a new tab from your browser and visit any page to see if it went back to default permalink settings. It should look something like this:
Step 5 : Only at this point go back to the Admin panel (Settings > Permalinks), and make necessary changes from the page and hit “Save Changes” again.
If you want to change the permalink for regular blog posts, make sure you are using proper structure tags or you might mess up everything real bad. You can keep the “Category base” and “Tag base” empty, if you really don’t need that. WordPress will use the defaults if you don’t setup anything. So, no worries.
Custom Post Type
Hmm, most probably this is the most complex part of all. If you are not expecting something really cool (custom taxonomy slug, Post ID etc. on URL), your default permalink structure should be something like this.
From this URL, you can easily guess that books is the slug for our custom post type and the rest is your post title.
Note: In case, if you have any fixed/custom slug (not structure tags) on your common settings (Settings > Permalinks Page) like this:
or on Optional section, like this:
You should use the following parameter on your custom post type register function’s arguments:
'rewrite' => array('slug' => 'books', 'with_front' => false),
This could be really useful, if you want separate the blog and CPT to be separate.
Custom Post Type Archive
If you want to have an archive section for custom post type make sure you have the following argument on your registration function:
'has_archive' => books,
In this example, we can see that our custom post type has archive and the slug is the same as the custom post type (books). So, the archive page for custom post type should be something like this:
It should work without any issue. However, make sure you have the appropriate archive page to display posts. In this case it should be archive-books.php.
Custom Taxonomy on Permalink
If you want to have categories on your CPT and want to show them on URL
or like this:
Registering custom taxonomy could be one of the best solutions you can possibly have.
Post ID on CPT Permalink
There are places, where we can really use the Post ID on the permalink structure, especially for Custom post types. Let me give you an example:
If you want to achieve something like this, you can easily do that following this post. This could be really useful in case if you want have a CPT for listings (businesses, directory etc.).