Take Control Over Post Ordering With Custom Keys

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

8 comments

  1. johnnyriss says:

    Greetings,

    Am I missing something on this post?

    On a site I’m building, I have a page where I’m showing a list of staff members. Each staff member is a different post. Post title is their name; bio and photo are in the post content; then I have their job title as a custom field. I also created a custom field called “sortorder” so that the client could add staff members and change the order if needed.

    The way I display the posts is I created a page called “staff-bios”. Then in my page template, I have a php statement that says if page = “staff-bios” do a query for posts in the category of “staff.”

    Now I just need a way to sort those posts based on the metadata key of “sortorder”

    I tried to use the code from your other posts with no luck. I’m in WP 2.7 so the post above is very intriguing.

    So, is the code missing above? Or is it me who’s missing something here.

    Thanks so much for your efforts.

  2. Brandon says:

    @johnnyriss – I’m not sure why the code we’ve posted previously didn’t work. It should have based on the information that you gave. Thank you for the catch. Something happened to the code snippit that we had placed here when we were editing the post. I’ve reverted the post back to its original state, so the code should be there. If you want to look more in depth at the method, please visit the WordPress Codex.

  3. Zergaloth says:

    Bit of a newbie to all this, but this post has been mighty helpful to me. Bit off topic, but I was wondering now, I’ve limited the number of posts that can be shown, but if the number of posts exceeds that limit, how can I add paging navigation?

  4. Brandon says:

    @Zergaloth – There are a number of paging type plugins on WordPress.org that you could look into, or there is also built in paging functions you can use. You’ll have to make sure you include the “paged” variable in your parameters for query_posts() though otherwise you’ll never be able to get past the first page of posts.

  5. juno106 says:

    Thanks this is really helpful. I am creating a site for a record company and want to be able to sort their catalog by release date (not post date). I created a custom field called release_date and have been entering the dates like this “November 20, 2003″ I cant figure out how to set the orderby code so it sorts it chronologically. I poured over the codex to no avail. Any pointers would be greatly appreciated. Thanks again

  6. Wes Bos says:

    Great post, this will help me out a ton!

    Is there a way that will let you list posts that have a meta field that is greater or equal to a variable.

    For example, set a variable to get todays time, and only display events that are in the future?

  7. Brandon says:

    @Wes – Yes, you can do all sorts of comparisons on the meta data. You just have to make sure that the value of the meta data is in a format that will allow it to sort how you want it to. Dates can be tricky because a value of 11/04/09 won’t sort properly.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>