Trying to order posts by a custom field, where a date i stored. The date looks like this "03.10.2019". All of the below function works properly, but the posts are sorted by post date, not custom field date...

// Events Archive Ordering
function leweb_get_posts( $query ) {

    // do not modify queries in backend
    if( is_admin() ) {

        return $query;


    // only modify queries for 'events' post type
    if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'events' ) {

        $query->set('orderby', 'meta_value_num');   
        $query->set('meta_key', 'event_date');
        $query->set('order', 'ASC'); 


    // return
    return $query;


add_action('pre_get_posts', 'leweb_get_posts');
  • 1
    SQL can't order by a date formatted like "03.10.2019". You would need to save your custom field as a SQL timestamp in order to be able to order queries by that field. – WebElaine Oct 2 '19 at 20:51

Thanks for the input, i solved the problem with the following workaround.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.