Ok. So, the most visited posts on this site all have to do with ordering posts by custom keys (here & here). We wrote some functions that made this super easy. Just copy/paste and you were ready to go.
Well, we came across a little gem on the WordPress Codex that greatly simplifies this process. Our original approach was to use WordPress’ built-in filter functionality to modify the query by adding in some special code. While this is still our preferred method when ordering posts within a certain category or something of that nature, there is now an alternate way to get this done. It turns out that the developers of WordPress have built this into a function call so that we can accomplish this task without having to modify the SQL. Ok, enough back story, here’s the code:
&orderby=meta_value&order=ASC'); if (have_posts()) : while (have_posts()) : the_post(); ?> ...some code to display the post...
That’s it! You can add this right into one of your custom templates or any other theme file and you’ll get a post listing ordered by your meta value. That’s pretty cool. You could also incorporate some conditional tags to display different ordering based on the category that you’re displaying. There’s a bunch of other things you can do with this function too. Take a look at the WordPress Codex page