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 http://example.com/design/design-principles.
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 http://example.com/development/design/ui/design-principles.
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.



June 7th, 2007 at 4:57 am
Well described and something that has plagued me for years.
I haven’t checked lately, but have they fixed the:
example.com/category/blahcat/post-title
icky? When using %category%, the permalink structure sometimes uses the word “category” in the link structure. HATE it.
I need to find out if that’s been fixed.
June 7th, 2007 at 11:15 am
Thanks Lorelle. I did not see “category” appearing while in this case. Must be fixed.
June 25th, 2007 at 7:34 pm
Thanks, this is a great explanation of how categories and permalinks in wordpress work.
June 25th, 2007 at 7:37 pm
@Lorelle
I think it hasn’t been changed. It happens when you click on a Category as to see that Category’s Archives.
Instead of saying http://www.site.com/mycustomcategory it says http://www.site.com/category/mycustomcategory
And yeah. I hate it. How can I change this?
June 27th, 2007 at 9:15 am
Hi all, I had the same issue initially, so I started using /.. as the category base.
, I had to resolve this.
This value does not trigger the default ‘category’ level and it works fine with the WP rewrite engine. The only adverse effect was that the category list URL links were showing as http://www.domain.com/../%category%/. This wasn’t an issue for my usual browsers (Safari and Firefox) as well a search engines which treat the /../ as a down level path back to the domain name level. But when i was told that the link wasn’t working in IE, thanks to Microsoft
The easiest solution I found, without having to mess with Wordpress functions, was to use a php ’str_replace’ function on the ‘include’ sections for all templates requiring it as well as the_category() tag to remove all occurrences of /..
All you need to do is to use the following code in your index (PHP 4, PHP 5):
For the includes:
For ‘the_category()’ tag:
The ‘category/’ path level seem to be added in the permalink_structure of Wordpress by default in order to avoid conflicts between page paths and category paths. I don’t think it would be there otherwise… so there is likely no “fix” for it coming up…
(I actually tried removing it from the classes.php file completely and while it gave me the proper end result for category urls it created an issue for single article views.)
With that in mind, if you decide to use this method, watch for slug duplicates between page slugs and category slugs or it will create a conflict for having two distinct functions to be triggered from the same URL (same slug name).
Other than that, this solution works fine for me.
June 27th, 2007 at 9:38 am
Yikes, the php tags omitted the code from my comment:
For the includes:
<?php
ob_start();
include (TEMPLATEPATH . “/sidebar.php”);
$sidebar = ob_get_clean();
echo str_replace(’/..’, ”, $sidebar);
?>
For ‘the_category()’ tag:
<?php ob_start(); the_category(’, ‘); $cats = ob_get_clean(); echo str_replace(’/..’, ”, $cats); ?>
Enjoy!
September 5th, 2007 at 1:41 am
[...] change-wordpress-permalink wordpress-categories-in-permalink-structure-behavior [...]
September 25th, 2007 at 4:31 pm
[...] http://ifacethoughts.net/2007/06/07/wordpress-categories-in-permalink-structure-behavior/ [...]
September 25th, 2007 at 4:32 pm
Great article! Thanks for the explanation, it helped me to solve a few headaches!
September 25th, 2007 at 4:34 pm
[...] http://ifacethoughts.net/2007/06/07/wordpress-categories-in-permalink-structure-behavior/ [...]
December 25th, 2007 at 12:20 pm
I think this plugin is better
http://wordpress.org/extend/plugins/top-level-cats/
February 4th, 2008 at 9:00 pm
[...] with WordPress’s category-based permalinks system: When a post has more than one category, the category with the lowest ID will be used in the permalink [...]
February 26th, 2008 at 7:15 am
[...] with WordPress’s category-based permalinks system: When a post has more than one category, the category with the lowest ID will be used in the permalink [...]
May 5th, 2008 at 5:43 pm
Hi.. I think I got lost somewhere… but I have this predicament.
My URL structure includes my category and hence reads this way..
“http://domain-name/category/post-title”…. but when I check out the categories, the category reads “http://domain-name/category/category-title” .. my question is… is it possible for me to change the URL to convert that into “http://domain-name/category-title”?
Thanks, I appreciate the help.
Roy
July 30th, 2008 at 7:38 pm
[...] more than one? If you use a “pretty URL” structure for your blog posts, do the multiple categories clash with that structure at all? I’m considering taking the category out of my permalink structure altogether to minimize [...]
March 6th, 2009 at 10:10 pm
I need to get the phrase ‘category’ back into my post permalinks. Does anyone know how to do this? If I add /category/ to the custom permalink it doesn’t like it, results in 404 pages. Help on this would be gratefully appreciated.
KJ
April 24th, 2009 at 6:29 pm
The problem I’m having is I want to select which category to use in the permalink, rather than have wordpress decide for me.
KJ you might need to clear some caching…
May 6th, 2009 at 6:40 pm
@ Roy Sencio, Lorelle, Gaston
Looking at your website, you still haven’t figured out how to fix the problem you are having with your category permalinks, where they look like this: “http://domain-name/category/category-title”.
I have had success with the “WP No Category Base” plugin (http://wordpress.org/extend/plugins/wp-no-category-base/).
I did not have to modify any code, I just activated the plugin and the default “category base” was gone from my blog forever.
Cheers,
Stu.