This came up while discussing the inclusion of category hierarchy in the WordPress post permalink structure. In the administration section, under Options -> Permalinks you can customize the permalink, also referred to as pretty URLs, you want for your posts. You can use the tag
%category% if you want to include the post’s categories in the permalink. So if you have a post categorized under design you would have something like
http://example.com/design/design-principles depending on rest of the customization.
So far so good. When you categorize a post under multiple categories, then WordPress has to choose which category to use to form the permalink. This is done by using the category with lowest category ID. You can see these IDs in the Manage Categories page. For example, if I categorize a post under design with category ID 11 and programming with category ID 12 then the permalink would be
Now, what WordPress does more is that it includes hierarchy of the category selected for the permalink. This hierarchy includes its parents, not the children. So, if I had a category hierarchy as development with child design which had a child category ui, and I categorized the post under design then the permalink would be
http://example.com/development/design/design-principles. Similarly if I had categorized it under ui instead of design then the permalink would be
But wait, there is more to it. Let us assume that the category development had a category ID of 5, design had 11 and ui had 15. Guess the permalink if I categorized the post under design as well as under its child ui. It would be
http://example.com/development/design/design-principles, that is without the category ui under which it was categorized. Why? Because, when WordPress sorted the categories for that post and picked up the one with the lowest ID, it picked up design. And it included design and its parents, that is development to be used for the permalink. The category ui was completely omitted. Similarly if this post had been categorized under development too, the permalink would be
http://example.com/development/design-principles, both design and ui would be omitted.
Note that this entirely depends on the category IDs. If the child had a category ID lower than that of the parent, the the permalink would contain both the IDs, whether you categorize in only the child or in both. For example, in our case, if development had a category ID of 21 and design had 11, and you categorized the post under both, the permalink would contain both
http://example.com/development/design/design-principles. This is because by the logic of selecting the category, design would be selected since it has a ID lower than that of its parent development.
To the normal user, this appears as if the category hierarchy works only in some cases. When the child category ID was lower than that of the parent, then the hierarchy would always appear. If not, and the post was categorized under the parent and the child both, the child would be omitted. Since category IDs are completely ignored by the everyday user, this behavior seems weird.
To further complicate this, WordPress 2.2 had a problem with category sorting (has been fixed) – they were being sorted in the opposite order. So, the category with the highest category ID was being selected to form the permalink. My blogger friend had this fatal combination, while it was fun to investigate it, it did cause some anxiety.
The solution that we arrived at was to never categorize in both the child and the parent categories. Then the behavior was consistent.
I am not sure how many of us use the category in the permalink. However, I came across at least two of such cases where the blogger was lost. I think WordPress behaves correctly, it is flexibility of the category structure that can overshadow this fact. Maybe a main category or something like that can be used to explicitly specify which category to use for a post.