Order Posts by a Custom Key

The WordPress query is a powerful thing. It’s at the core of every request a visitor can make on your website. Whether someone is visiting your blog, your about page, or a custom post type, when the request is made it will eventually run a WordPress query. In some cases, you’ll want to change the order in which WordPress displays your content. WordPress is flexible enough to handle this because of the actions and filters available through its Plugin API.

Which Way To Order

WordPress offers a number of filters to modify the query. Which one you choose depends on how you want to modify the query. In our case, we want to change the order of the content we’re getting back. When changing the order, there are two aspects that we need to keep in mind. The first is what data we want to order the content on. You can sort your content on things that WordPress provides for you like the date, the title, or the page order. The second piece of information is which direction we want to order our content.

You can think of this in terms of one of two values, ascending or descending. A to Z would be ascending, and Z to A would be descending. Because WordPress got its start in the blogging world, the default ordering takes its cue from blog posts. Blog posts are chronological in nature and therefore are ordered by date with the newest content first. In programming terms, we think of this as descending by date.

Order By Custom Key

Often times when we’re developing a custom site for our clients, we need to allow for ordering content by a custom key. For example, let’s say that we build a custom post type that needs to dynamically show the most popular piece of content based on a star rating. In this case, we would build a system for visitors to rate the content with that rating being stored as meta information related to the post. There are a couple of different ways to do this.

Adding The Custom Key To The SQL Query

One way would be to use specific filters to modify the SQL query to join the post meta table and order by the newly selected field. Let’s take a look at how to accomplish this.

Using WP_Query Abstraction Layer

While it is possible to modify the SQL query directly as shown above and achieve our desired results, that method is prone to breakage because the SQL query may change at some point. It’s more in alignment with the WordPress best practices to use the WP_Query wrapper to handle the internals for you. Using the WP_Query object as an abstraction allows you to modify the internals without needing to understand SQL (because WordPress core developers have done all the hard work for you). Here’s how that would look.

The Best Way To Order By Custom Key

As with most things in WordPress, the method you use will be defined by your specific needs. WordPress is flexible and powerful enough to allow you to modify queries directly. But as Spider-man’s story reminds us, “with great power comes great responsibility.” If you do modify queries directly, you’ll need to be much more careful when you update things. Using core WordPress APIs will keep your code lean and solid because the WordPress core team works hard to keep them up to date with any new changes.

Similar Posts