ifacethoughts

WordPress Categories In Permalink Structure Behavior

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.

Discussion [Participate or Link]

  1. Lorelle said:

    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.

  2. Abhijit Nadgouda said:

    Thanks Lorelle. I did not see “category” appearing while in this case. Must be fixed.

  3. Gaston said:

    Thanks, this is a great explanation of how categories and permalinks in wordpress work.

  4. Gaston said:

    @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?

  5. Hexalys Design said:

    Hi all, I had the same issue initially, so I started using /.. as the category base.
    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 🙁 , I had to resolve this.

    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. 🙂

  6. Hexalys Design said:

    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!

  7. Get multiple DMOZ links and use Wordpress to do it | Needless Productions said:

    […] change-wordpress-permalink wordpress-categories-in-permalink-structure-behavior […]

  8. Wordpress Permalinks Structure said:

    […] http://ifacethoughts.net/2007/06/07/wordpress-categories-in-permalink-structure-behavior/ […]

  9. Copes Flavio said:

    Great article! Thanks for the explanation, it helped me to solve a few headaches! 🙂

  10. Struttura dei Permalinks di Wordpress said:

    […] http://ifacethoughts.net/2007/06/07/wordpress-categories-in-permalink-structure-behavior/ […]

  11. atshya said:

    I think this plugin is better

    http://wordpress.org/extend/plugins/top-level-cats/

  12. WordPress SEO Tip: Implementing the Power of Theming and Siloing | SEO Design Solutions - Internet Marketing Blog said:

    […] 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 […]

  13. SiteProNews » Blog Archive » WordPress SEO Tip: Implementing the Power of Theming and Siloing said:

    […] 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 […]

  14. Roy Sencio said:

    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

  15. Categories & Tags | GuitarAngel.net said:

    […] 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 […]

  16. KJ said:

    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

  17. Vince Samios said:

    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…

  18. Stu said:

    @ 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.

  19. Wynne said:

    Indepth post. It made my eyes boggle for a moment, but I had to re-read a couple times before ‘got’ what you were saying. But it makes a lot of sense. Just make sure to categorize in one category only, and not in both child and parent categories (which is what wordpress tries to do).

  20. Ashish said:

    Very well described article. I was wondering why the wordpress it taking permalink in absurd way but the fact is that i was selecting multiple subcategories and parent categories for my posts.

    Thanks a lot man. You saved my day.

  21. Why China Sucks said:

    Thanks, I shall try this to solve my issue. It’s really annoying!!

  22. Rate My Biceps said:

    great post, very informative in helping me fix my problem with some backlinks.

  23. Categorizing In WordPress Parent Categories And Using the_category | iface thoughts said:

    […] And if you are thinking that there is no harm in categorizing in the parent categories, there might be unintended side-effects. […]

Say your thought!

If you want to use HTML you can use these tags: <a>, <em>, <strong>, <abbr>, <code>, <blockquote>. Closing the tags will be appreciated as this site uses valid XHTML.

freshthoughts

contactme

Abhijit Nadgouda
iface Consulting
India
+91 9819820312
My bookmarks

badgesand...

This is the weblog of Abhijit Nadgouda where he writes down his thoughts on software development and related topics. You are invited to subscribe to the feed to stay updated or check out more subscription options. Or you can choose to browse by one of the topics.