WordPress 2.2 Changes Category Order Behavior?
Posted May 25th, 2007 @ 08:57am by Erik J. Barzeski
Though I don't assign multiple categories to posts here at nslog.com, 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 http://thesandtrap.com/reviews/
).
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:
http://thesandtrap.com/tags/reviews/eagle_sticks_golf_club_zanesville_oh_review
versus
http://thesandtrap.com/courses/eagle_sticks_golf_club_zanesville_oh_review
http://thesandtrap.com/tags/hot_topics/the_cost_of_being_a_fan
versus
http://thesandtrap.com/the_numbers_game/the_cost_of_being_a_fan
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.
Posted 25 May 2007 at 11:42am #
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.
Posted 25 May 2007 at 1:46pm #
I thought I was smoking crack, couldn't figure out how I "broke" the category order. Thanks for the insight.
Posted 28 May 2007 at 2:11am #
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.
Posted 14 Jun 2007 at 2:14pm #
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?
Cheers
Posted 19 Jun 2007 at 5:46pm #
[...] 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 [...]
Posted 17 Aug 2007 at 3:20pm #
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!