Sort Posts With The Help Of Custom Fields

WordPress-Christmas-17Quite often, a WordPress developer likes to have posts sorted according to a custom field. If you would write a SQL-statement, it would be quite complex, since you need to JOIN two database tables (wp_posts und wp_postmeta).

But there is a much easier way, if you use the template tags provided by WordPress - in our case we use the function query_posts. Below you can see an example, which includes a custom field in the query, and also sort the posts with the help of this custom field. A short, easy to understand code with effective result:

Bildschirmfoto 2009-12-04 um 21.49.05


Guest Post

This post is written by Sergej Müller and is a post in our Advent Calendar on WP Engineer about WordPress.
Thank you very much from my part to Sergej.
If you also like to have your interesting post published on our website, please let us know on our contact page. Of course we will appreciate your contribution!