Separating Blog & Custom Post Type Permalink
If you have custom post type registered on your WordPress based site, it automatically separates the blog section/Posts from your CPT posts. Regardless of the settings for your permalink structure, CPT would follow its default permalink structure and regular permalink structure would have no effect on CPT posts. Let me give you an example. Let’s say your general permalink structure has been set to something like this:
Also assume that you also have a custom post type called “Books” and your post register functions has “has_archive” parameter set to true/books and the rewrite parameter has set the slug to “books”. In such case the permalink structure for your custom post type should be something like this:
This url would act as the archive page for books post type. And the following permalink for any single post that belongs to your books post type.
Now, lets take serious look at the top two URL and the bottom two for the CPT. Did you see the fundamental differences between them?
Top two URL was for the “blog” section our site. However, the permalink structure doesn’t quite say so. Wouldn’t it be better if we had the following permalink for the Blog/ Posts section?
Blog Home Page
For single blog post
For Blog Categories Archive
For Post Tags Archive
At least we would had a “blog” section entirely separated from any other URLs.
How do we do that?
Step 1: The very first thing you need to do is to create a page with the “Home” title and publish it. If you want to customize the look and feel, you can use any custom template you want.
Step 2: Create another page with “Blog” title. Don’t type anything else on that page, from the template section select “default template” and simply publish it. We are doing this to make sure that “index.php” file of your theme would be the home page for blog section and regular loop would show the latest blog posts.
Step 3: Now lets go to “Settings” menu and select “Reading” page. From the “Front page displays” section select the “A static page (select below)” radio button. For “Front Page” select the “Home” page that you just created and for “Posts Page” select the “Blog” page.
Step-3: Now, lets head toward the “Settings” menu and click on “Permalink”. From the “Common Settings” section, select “Custom Structure” radio button and type /blog/%postname%/ on the input field. This is to make sure that the single blog post of your site would be something like this:
However, you can change the “postname” structure tag with something else. Check out the list of Structure Tags that can be used here.
Now, go to the “Optional” section and for “Category base” type blog/category and for “Tag base” type blog/tag. Now simply click on “Save Changes” button. That’s it. You are done.
Important note: Above settings would force your Custom Post Type (Books) to have “blog” slug ahead of the URL and it would look like:
To get rid of the “blog” slug from the URL, make sure you have the “with_front” parameter from “rewrite” argument set to “false” on your custom post type register function. Here is the sample:
'rewrite' => array('slug' => 'books', 'with_front' => false),
This would give you cleaner permalink structure for both the blog section and for CPTs.
Once you are done with steps mentioned above, simply go to the front end and refresh the page. Now, you should have totally separated URL structure for both the CPT and Blog. Hope enjoyed.