Sort Categories by Name or ID in WordPress

When I started working with my WordPress blog, I noticed that my Category listings on the sidebar show in a disordered way. I tried to dig more about this and there are loads of similar questions found in the WordPress Support forums, but no clear answers were there. After checking loads of pages, I could manage to find out some solutions which handle the issue. I would love to share it with my readers, and I hope some of you may find it helpful.

How to Sort the Category Listings in WordPress?

Depending on the Theme you use, the default display of your Categories may vary; sometimes it lists in the alphabetical order, sometimes on Category_IDs, Sometimes according to your number of posts inside, sometimes all. There are two ways you can handle the Category Listings.

  1. Change the code in your Sidebar.php page
  2. Change the code in “wp-admin/admin-functions.php”

Sort WordPress Category Listings by altering Sidebar.php

Go to the Presentation tab after you login to your WordPress, Under Theme Editor Tab choose the ‘Sidebar.php’ file from the list there. Locate the code which calls the Category List – by default it will be – replace that with the following code:

  • For Sorting Category List by Name:
    • <?php list_cats(FALSE, ‘ ‘, ‘name’); ?>
  • For Sorting Category List by ID:
    • <?php list_cats(FALSE, ‘ ‘, ‘name’); ?>

Sort WordPress Category Listings by altering admin-functions.php

In “wp-admin/admin-functions.php” at line 590, change:

  • For Sorting Category List by Name:
    • $categories = $wpdb->get_results(“SELECT * FROM $wpdb->categories ORDER BY cat_name”);
  • For Sorting Category List by ID:
    • $categories = $wpdb->get_results(“SELECT * FROM $wpdb->categories ORDER BY cat_ID”);

A Word from the Author:
I hope either of these should solve the issue, and your WordPress Blogs are showing Categories in a Sorted way. I will be searching for more solutions like these, and whenever I find one, I will surely be adding to this.

Leave a Reply