Support » Plugins » Alphabetical Sorting of Categories

  • Okay, so I was adding a new category “Photoblography” to my WordPress installation… and much to my dismay, it was displayed LAST in the category listing with no front-end way to make it appear under “Personal”… where it belongs… with the “P’s”.
    So that means getting under the hood. Oh joy.
    After 15 minutes of digging around, I found the bug (in my mind anyway) and fixed it. If you want your categories to display properly in alphabetical order and NOT in order of when you added it, then do this REALLY REALLY simple fix to the following file: template-functions-category.php which can be found in /wordpress/wp-includes/
    Around like 290 or so, you’ll see these lines of code:
    $query = "
    SELECT cat_ID, cat_name, category_nicename, category_description, category_parent
    FROM $tablecategories
    WHERE cat_ID > 0 $exclusions
    ORDER BY $sort_column $sort_order";
    * please note that “category_description, category_parent” should be on the same line as its previous line. Because of the size of this column, it wraps around.
    On the last line, remove $sort_column $sort_order and replace it with cat_name. That line should look like this:
    ORDER BY cat_name";
    And whallah, your categories will display as they ought to… alphabetically.
    If you’re concerned about getting rid of the code permanently, simply do a little commenting out and rearranging so your code looks like this:
    $query = "
    SELECT cat_ID, cat_name, category_nicename, category_description, category_parent
    FROM $tablecategories
    WHERE cat_ID > 0 $exclusions
    ORDER BY cat_name";
    ### hack by michael -- original below
    //ORDER BY $sort_column $sort_order";

Viewing 9 replies - 1 through 9 (of 9 total)
  • I agree, it makes sense to have the default list in alphabetical order.

    Actually, I found code in the Wiki that I used on my site; just a basic parameter for the category tag, and it now lists my categories in alphabetical order.
    <?php wp_list_cats(“sort_column=name”); ?>
    Hope that helps!

    By the way, I found all the available parameters available here:

    It’s some of those “cryptic” details that mildly annoy me about WordPress. Unless you’re digging for it, you wouldn’t know that it exists… and you have to dig.
    From a general end-user’s perspective, there should be a few interface-related changes in WordPress’ back-end management. Sorting should be toggled with the click of your mouse, not by digging in templates or code and changing values that are unknown to the average “afraid-to-dig-or-mess-with-code” users. I’m not refuting how cool WP is or how powerful it can beâ€?but there are a few things about it that really could be made a bit more “intuitive” or self-explanatory. Hunting for or modifying a PHP function isn’t as intuitive if one isn’t familiar with PHP, let alone the database structure.

    I hope you will forgive me for saying this but somehow I find the phrase *in WP there should* etc incongruous. Why should there?

    Ok, let me break it down for you.
    I’ve been working with various web development companies and clients on interface-related issues, and I’ve found that the more efficient you make your layout and how things operate (simple single-click/toggle verses keying in your sort order) the easier it is for your end-users to figure it out. Afterall, isn’t that one of the goals of WordPress, to provide a great, easy-to-use, and powerful blogging tool into the hands of a variety of users throughout the globe?
    I simply say “should” because it’s an expressed opinion about what I think could be done to improve WordPress and make it an even more favorable application. Just because I say WordPress “should” do something, doesn’t mean they have to, will, or even think about doing it. It’s like me saying we “should” get the troops out of Iraq before the end of theyear… we should, really… but my saying so isn’t necessarily going to change the minds of the current administration.
    Oh, and I find nothing incongruous about my remarks. If you’re going to find my phrase(s) incongruous, at least share your thoughts as to why you think so… otherwise you leave the rest of us wondering, “huh?”

    If enough people ask for a feature or a particular behavior of the software, it “should” be considered for addition to the software as an option (in the case where the original behavior is still desired) or a replacement to the original functionality (where the original behavior is not deemed useful). I’ve seen the request for the ability to define the order of the categories several times already in this forum since people aren’t satisfied with the default search order.
    I had to hack my index.php file to change the sort order, but I would have preferred to choose the sort order in the categories administration page.
    The full-featured answer would be to allow the admin to choose to sort by id, alphabetically by name, or by admin-defined order. True implementation of the admin-defined order would include a field for each record in the category table to hold the order index plus an interface in admin to allow admins to easily move categories up or down in a list to define the order.
    Right now, it seems people are hacking the admin-defined order by editing their index.php to use alphabetical by name sorting, and then adding spaces to the beginning of their category names to make them sort the way they want. The spaces don’t show up in the list of categories, but the more spaces you have in front of the category, the higher it will appear in the sorted list.

    but the more spaces you have in front of the category, the higher it will appear in the sorted list.

    That doesn’t seem to be working for me. I’m adding spaces using the admin Categories screen and edit function, putting spaces in front of the titles. Is that how it’s supposed to be one?

    Hi —
    A big tip o’ the cap to Michael for digging through the code and putting up this suggestion. I just made the alteration and it works like a charm. As time goes on, my blog (SexBizBlawg) will have a large number of categories, and this makes it look much better.
    As a member of the code-challenged, I appreciate the help of this community. Thanks again.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Alphabetical Sorting of Categories’ is closed to new replies.