I’ve been working recently on the portfolio section of this blog. It uses a custom post type I created that I’ve got installed as a plugin.
One thing that always concerned me was ordering. When you create a page, you can use the “order” box on the right side and then use this when you query the database. When you add a post, you don’t get this option, and I’ve not found a way to add this ordering functionality into my custom post type framework yet.
Custom post types allow you to add any custom fields, or your own fields. This means that you could have a ranking value in one of those custom fields or your own custom area.
Previously, when I’ve wanted to order this, I’ve always loaded all the posts, then gone through each of them and taken the custom fields – ordering an array.
Instead of this long winded and heavy scripted code, you can actually “orderby” a custom field.
1 2 3 |
< ?php $posts = query_posts($query_string. '&orderby=meta_value&meta_key=ranking&order=DESC' ); ?> |
Tell query_posts to “orderby” a meta_value, and then tell it which meta_key to use. Here, you’re ordering by a custom field “ranking”.
Image Credit: Whiskeygonebad
Comment or tweet @douglasradburn