As I was doing some development of a WordPress plugin where custom post types were being added and also add and use a custom taxonomy, I bumped into the problem of getting "nice" and "flexible" permalink structures in WordPress. If you find this topic confusing, read the whole blog post as it will hopefully be more clear to you when you get through a few of the examples.
If you are new to the subject, WordPress uses a flexible taxonomy structure so that you can create your own taxonomies, similar to how you can create your own custom post types. The standard taxonomies in WordPress are "category" (which is a hierarchical taxonomy) and "tags" (which is a non-hierarchical taxonomy). More on this later.
Initially I thought it would be simple to add a structure like such: example.com/post-type-slug/custom-taxonomy-category/
So for example: if you have created a custom post type called "books", you may want to have a custom taxonomy called "genre". The structure I wanted was:
example.com/books/genre/fiction
and then you could add more categories like:
example.com/books/genre/nonfiction
example.com/books/genre/biographies
and so on.
(Maybe just example.com/books/fiction would be quite cool ...)
I thought this would be easy to achieve but apparantly I couldn't find any sources that supported the claim that this type of more flexible type of structure was possible to create: but rather the opposite. WordPress doesn't seem to support this, but rather rely on having all slugs for everything on one "level":
example.com/books/ is fine
example.com/genre/fiction is fine
As you can see: as long as you don't try to "build" on that "category" taxonomy-slug, it will work and you will need to separate the custom post type slug and the custom taxonomy slug - since they're presumably in the same namespace.
So how would you distinguish "genre" from a "custom" taxonomy for pages with the same name?
For example if you created a different custom post type called "movies" and wanted a custom taxonomy for that as well, also called "genre": then I believe that would not work (if they have the same slug) and they could not have the same slug name, occupying the same namespace.
The solution I found was to simply prepend the custom taxonomy slug with "book-": "book-category":
example.com/book-category/
example.com/book-category/fiction
and so on.
The slug for custom post is still books:
example.com/books/
and the posts will follow this structure primarily:
example.com/books/bookpost
Although example.com/book-category/fiction/bookpost will redirect to example.com/books/bookpost.
So as you can see custom posts share the namespace with other custom posts for taxonomies and the standard taxonomy "categories" which exists for "pages" and "posts", and "tags".
Since a constraint was to not introduce additional plugins, I chose this option, in trying to understand the namespace and by doing so knowing how to avoid duplicate taxonomy slugs. Another option would be to use one of the permalink structure plugins that exists. There are a few, but the one that worked best for me was wp better permalinks.
There are a few others, some free and some at a cost. They can be worth checking out but wp better permalinks was the easiest one to use in my case.
With this structure, you can get a nice permalink structure which in my view, is a bit easier when you want to organize things.
Okay, so I said I would get back to hierarcical and nonhierarchical taxomonies. When should they be used? Well, "categories" are a typical "hierarchical" taxonomy. Using that you can sort your posts into nice categories, which can be multilevel, as in a tree structure: top level categories and sub categories beneath them. Nonhierarcical taxonomies, like "tags", doesn't have any levels, it's just one "flat" level, where you add "tag words" to your posts (which is also a way or sorting your posts).
The permalink structure would be something like this:
example.com/book-tag/funny
example.com/book-tag/sad
and so on. On a more practical side, given that you are just going to use one level of "hierarchy" then "hierarchical" taxonomys (like categories) have a quite "fixed" UI where you edit the posts: the categories will show up with checkboxes which you can check, in order to assign a post to a category. Tags on the other hand has an auto-complete type function, where you will have to type (if the tag doesn't exist yet) or start type the whole tag name in order to assign it to a post.
Not a big deal, but I prefer the category UI. You can of course also use both hierarchical and nonhierarchical taxonomies for your custom post type at the same time. Now this makes it a bit more "meta data" to add to each post but they will be "doubly sort-able", if you like those kinds of things.
Whereas "category" can be used for fixed types like "fiction", "nonfiction" and so on, "tags" can be used to add more "content related" keywords, like "funny" or "sad", or whatever you think is a good keyword for your post content.
I hope this blog post clarifies some things about (custom) taxonomies and permalink structure in WordPress. There's some additional details about things like SEO, redirects, what templates to use and how a query_var-version of permalinks also can be used as an URL. But hopefully I will get back to that in a later blog post.











