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.

The Most Common Conversion Mistakes Part 2

The Most Common Conversion Mistakes – Part 2

Wondering why your website isn’t getting the results you want? It could be that you’re making one or more of the most common conversion mistakes. “Conversion” refers to the percentage of your website visitors who take a desired action, such as clicking, buying or downloading. In a previous post we talked about some of the…
Read More
Writing for Websites

Writing for Websites

Jen Miller is the founder of Need Someone to Blog, a content marketing agency located in Orange County. Jen is also the mother of eight, one of the lead organizers of the Women Who WP meetup, on the organizing team for WordCamp Orange County 2017, and is speaking at WordCamp Europe on SEO and highly…
Read More
how-to-perform-a-website-audit_sidebar

Maximize your website's appeal and keep everything working for the best conversions.

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.