Artikel einer Kategorie nach Inhalt eines Spezialfelds sortieren

In WordPress sollen veröffentlichte Artikel einer bestimmten Kategorie, die allesamt ein Spezialfeld (Custom Field) haben, das ein Datum im Format JJJJ-MM-TT, also z.B.: 2009-12-20, enthält, nach eben diesem Spezialfeld aufsteigend sortiert ausgegeben werden, sofern das Datum nicht bereits in der Vergangenheit liegt.

Sort articles with a custom-field that includes a date in the form yyyy-mm-dd by this very custom field, if the articles are published, are all in the same category, and the date in the custom-field is now or in the future.

Hier die Lösung (und hier auf Pastebin):

<?php

    $querystr = "
            SELECT * FROM $wpdb->posts
                LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
                LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
                LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
                WHERE $wpdb->term_taxonomy.term_id = [[ Category-ID ]]
                AND $wpdb->term_taxonomy.taxonomy = 'category'
                AND $wpdb->posts.post_status = 'publish'
                AND $wpdb->postmeta.meta_key = '[[ Name of Custom Field ]]'
                AND STR_TO_DATE($wpdb->postmeta.meta_value, '%Y-%m-%d') >= NOW()
                ORDER BY $wpdb->postmeta.meta_value ASC
        ";

        $posts = $wpdb->get_results($querystr);
        foreach ($posts as $post) :
                setup_postdata($post);

?>

<!-- Whatever shall be displayed -->

<?php endforeach; ?>

Super, nicht?