How to Control Sort Order in Category

Posted on August 30, 2007
Filed Under WordPress Coding, WordPress Customization |

Category sort order had been on WP blogger’s wish list for a while. People want an option to specify sort order on each category item, just like what we can do for pages, — where you can give each page a Page Order number to display pages in the order you want.

It should be fairly easy to add this feature in WP, just an additional column in wp_categories table will do. That makes me wonder why it is not there yet. Anyway, in the mean time, there are several solutions to deal with this issue.

1. Plan ahead to enter category items in the order you designed

Category items can be ordered by the ID, which is assigned in the order of data entry. If you want an item to show first, then enter it first. For example, if you have a blog with career related topics and you want to have category items in the following order:

Career Advice
Resume Samples
Interview Tips
Career Path

Then you just enter these items in the above order one by one. If you specify ’sort_column=ID’ in wp_list_cats function, the category will show as the order of your data entry:

<?php wp_list_cats(’sort_column=ID’); ?>

2. Numbering category items

Category can be sorted by name. If you already entered a bunch of items and want to sort them, you may add a number to each item like:

1. Career Advice
2. Resume Samples
3. Interview Tips
4. Career Path

Then when you specify <?php wp_list_cats(’sort_column=name’); ?>, the category will show in the order as you numbered. If you have over 10 items, you may want to number them as 01., 02, 03, …, or use letters as a., b, c, …

3. Rename category item based on alphabetic order

It may be possible to adjust category names to make it show in the sort order you want. For example, if you order the following item by name, it will show as:

Career Advice
Career Path
Interview Tips
Resume Samples

What if you want them to be sorted as:

Career Advice
Resume Samples
Interview Tips
Career Path

You can rename some of the items to be:

Career Advice
Resume Samples
Tips for Interview
Your Career Path

4. Reset cat_id by database update

You can manipulate WP tables directly to change the ID for category items in order to control the sort order. Login to web hosting’s cpanel and to to phpMyAdmin where you can do all kinds of database activities. For example, you can change a category item’s ID from 5 to 10 by SQL update as:

update wp_categories set cat_id=10 where cat_id=5;

Since posts are associated with category by records in wp_post2cat table, you also need to update this table as:

update wp_post2cat set category_id=10 where cat_id=5;

If you already have existing category in ID 10, you need to move data on ID 10 first, otherwise it will be overridden.

5. Hard-Code category link

If your category is stable, you may just hard-code them in the side bar. Instead of using
<?php wp_list_cats(’sort_column=ID’); ?> which shows entire list of category
you can code each item as:
<a href=”/category/advice/” title=”Career Advice”>Career Advice</a>

Comments

One Response to “How to Control Sort Order in Category”

  1. How to Control Sort Order in Category | Careers Resource Center on August 31st, 2007 3:49 pm

    [...] the original here: How to Control Sort Order in Category advice, career, interview, items, rename, resume, samples, sortedadvice, career, interview, items, [...]

Leave a Reply




*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word