Order Posts by a Custom Key

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.

Written by the Team at Pixel Jar

We hope you got something useful out of that post. If you'd like to read more we have an active blog with topics across the spectrum of website development. If you're researching information for a project we'd love to talk to you about it.

Beaver Builder responsive headers

Beaver Builder Responsive Headers

Creating custom responsive behaviors with Beaver Builder is a snap. In this post, we’re going to look at Beaver Builder responsive headers and a good method for managing content between breakpoints. Beaver Themer is the Key to a Responsive Header Beaver Builder is a powerful tool and you’re able to create a lot of flexible…
Read More
Setting your Digital Strategy for 2019

Setting your Digital Strategy for 2019

The calendar has turned and another year has begun. What are you most excited about? What gives you the biggest pause? How will you spend your time? What are your goals, your resolutions, your ambitions? Do you expect to reach new heights and try new things? Or are you anticipating 2019 will look a lot…
Read More

Learn how to understand your website's strengths and weaknesses, and how to make changes for the best possible chance at success.

Download Now

How Can We Help You?

We want to build your next project.

Connect with Pixel Jar

Our Community

Subscribe to learn more about the goings on at Pixel Jar.

  • Note: Your email will be added to our CRM and be used to receive emails from Pixel Jar. You can unsubscribe at any time.

  • This field is for validation purposes and should be left unchanged.