Subscribe to
NSLog(); Header Image

WordPress 2.2 Changes Category Order Behavior?

Though I don't assign multiple categories to posts here at, I do at The Sand Trap in two instances: for "Hot Topics" and "Reviews." The former helps me fill out the "Top Stories" section in the sidebar and the latter simply helps me direct people to our reviews (via

I discovered in WordPress 2.0.x and 2.1.x that categories were sorted by their category ID (cat_ID) in an ascending order. On posts with multiple categories assigned, cat_ID 4 took precedent over cat_ID 5 or 6 or 99. This precedence exhibited itself in two places: building of permalinks (URLs) and the category list (via WordPress's the_category() function).

I don't care about the latter much, but the former is important to me. I don't want "Reviews" or "Hot Topics" to be used to build URLs, so I set their cat_IDs to 98 and 99 respectively (with a parent category, "Tags," set to 100). All other cat_IDs are lower, and life was good. URLs were "properly" built based on the behavior I saw in WordPress 2.0.x and the behavior continued when I upgraded to WordPress 2.1.x.

With WordPress 2.2, this behavior has flipped around. No longer are categories listed in ascending order, but rather in descending order. This change in the way WordPress behaves effectively changed every URL on my site which has multiple categories. This goofs up things like Pingbacks, not to mention the fact that URLs are no longer the way I want them. The old Permalinks still work fine, but the "new" (and incorrect) permalinks are pushed out by the system wherever it has control over link generation.

This error, bug, or "accidental change in behavior" exposes a nasty flaw in WordPress - the inability to set a "primary" category. MovableType had this feature, and it worked as it should. WordPress lacks this feature, and now the behavior I relied upon has been changed for seemingly no reason.

Put another way: permalinks (URLs) used to be built from the category with the lowest ID. Now they're built from the category with the highest ID. Category lists used to be in "low to high" order, and now that order too appears reversed.

Some specific examples:

I'm not sure where this changed in the WordPress code base (or, again, why), but I do know this is not the result of some plugin or any modifications I've made.

I suspect I could likely "solve" the problem by manually editing my database (both wp_categories and wp_post2cat) to move categories with IDs 1 to 33 to be higher than 100, but that's a lot of work for what is probably a simple "ORDER BY _____ DESC" somewhere.

If anyone can shed any light on this subject, I'd appreciate it. Trac ticket is here.

6 Responses to "WordPress 2.2 Changes Category Order Behavior?"

  1. I believe I've worked around this - for now - by setting the categories with cat_ID of 98, 99, 100, and 101 to their negatives: -98, -99, -100, and -101. Corresponding updates to wp_post2cat were relatively easy: UPDATE wp_post2cat set category_id = '-99' WHERE category_id = '99'.

    I still believe this type of behavior is a bug - the behavior changed, unannounced and I believe unintended - and it certainly highlights the need for a "primary" category feature since categories are (often) intrinsic to URL-building in WordPress.

  2. I thought I was smoking crack, couldn't figure out how I "broke" the category order. Thanks for the insight.

  3. Icky bug. Here is a patch for 2.2. What happened was that categories were re-ordered by name for display purposes, and that ended up choosing new primary category for category-based permalink setups as a side effect. My patch has get_permalink() sort the category list by ID, so no matter what order they are in when it gets the list, it'll sort by ID before choosing a category.

    This fix will be in 2.2.1 (and 2.3).

    Your point about being able to choose a primary category is well taken. This has irked me for a while (to the point where I recommend people not use category-based permalink structures). It's not immediately obvious how the UI for such a feature would look and work without a lot of DHTML, but I'll think on the matter.

  4. Sorry to be a bit daft here, but I can't figure out how to apply the patch. I've tried to add the blocks of code at the end of the pages you have listed before it (link-template.php, category-template.php), but it creates errors.

    Could you give me a rough idea of how I should apply this patch?


  5. [...] Permalinks Migration - This plugin allows you to change your permalinks structure. Due to a fairly severe bug in WordPress 2.2 I decided to change my permalinks structure away from using the category in my URLs. This plugin [...]

  6. Hello,
    I have developed a custom plugin to do just that - specify a custom primary category that is used to generate the permalinks
    NG Custom Category WordPress Plugin
    Any suggestions are welcomed!