WP_Query ordered by custom field that is a date string?

The WP_Query page on the Codex has a section on Order & Orderby Parameters.

If you want to order the query by a custom field you should add 'orderby' => 'meta_value' AND you must also specify the custom field (called your_date_field in the above example) using the meta_key key in the query.

$timeline_query = new WP_Query(array( 
    'post_type' => 'post', 
    'orderby' => 'meta_value',
    'meta_key' => 'your_date_field' 
    'posts_per_page' => -1 
));

Leave a Comment