Organize WordPress Terms in Alphabetical Order

March 7, 2014

I’d argue that, at this point in time, working with WordPress taxonomies has never been easier.

Sure, creating, tagging or categorized, and adding custom taxonomies to posts and custom post types has always been relatively easy, but when it comes to implementing a solution that utilizes custom post types and/or custom taxonomies, then constructing queries can sometimes be a bit of a pain.

Of course, there are still cases in which retrieving and organizing data still leaves something to be desired.

I don’t fault WordPress for this, though. It’s more of a function of how data is ordered within databases, applications, and in computers in general.

WordPress Terms in Alphabetical Order

Specifically, one example is trying to order terms in alphabetical order then the actual terms are named something like this:

Acme 1

Acme 2

…

Acme 9

Acme 10

Acme 11

For anyone who has dealt with data like this in the past, you know that when you query the information and you ask to order it ascending order, you’re going to get it back like this:

Acme 1

Acme 10

Acme 11

Acme 2

…

…and so on.

Sure, there are plenty of algorithms that are available in order to sort the information in alphabetical order, but PHP does have a built in function that makes this trivially easy given an array of information.

A Concrete Example

Usually, I find that seeing this material in the context of a concrete example helps to make it a bit more clear. To that end, let’s say that we have a book composed of chapters.

To that end, we’ll have a taxonomy called book and its children will be chapters. Then, the steps will be as follows:

The comments should provide enough information for exactly how this works. Note that I’m also aware there are improvements that can be made, but for the sake of making the example as clear as possible, I’ve opted to share it as is.

I’d rather people understand the point of what I’m trying to share rather than demonstrate how compact code can be.

But This Isn’t The Only Option

Here’s the thing: this strategy works well if you have a relatively small data set and if you’re okay with using the term IDs and term names to help organize your information.

I’m not making the case that this is the best way to do this, or that it’s the primary option as it relates to ordering data – there are always alternatives and they are unique to your situation.

However, if you’re looking for a quick way to naturally place taxonomy terms in alphabetical order, have a relatively small data set, and don’t mind working with an associative array, then the above solution is one that works well.

If you use array_push() to add one element to the array it’s better to use $array[] = because in that way there is no overhead of calling a function.

So using array notation is actually more performant (well, we’re comparing milliseconds, really, but still), but when it comes to things like this, I think that it’s more verbose and easier to read (especially for those who are just starting out).

For the sake of a demo and readability, I’m fine with `array_push`, but your way is the way that you’d likely see me doing it in production.

Just Getting Started with WordPress?

I write a lot about WordPress development but if you're just getting started, I recommend checking out WPBeginner. They have free training videos, glossary, and more.

WPSessions hosts some of the best WordPress training you’ll find anywhere from many well-known speakers.

If there’s something you’d like to learn, and it’s not already covered here, it’s probably been covered at WPSessions. If you use the special link below you can watch your first session for free and get a steep discount on the full-access VIP membership.