How to change main query based on post meta

Finally got it. This code in functions.php:

function set_order_by_last_date($query){

$query->set( 'orderby', 'eventstart');
$query->set( 'order', 'DESC' );
$query->get_posts();
if ($query->have_posts()){
    if ( eo_get_the_end('Ymd',$query->posts[0]->ID,$query->posts[0]->occurrence_id) > date('Ymd') ){
        $query->set( 'event_end_after', 'today' );
        $query->set( 'orderby', 'eventstart');
        $query->set( 'order', 'ASC' );
    } else {
        $query->set( 'event_end_before', 'today' );
        $query->set( 'orderby', 'eventstart');
        $query->set( 'order', 'DESC' );
    }
}
return $query->query_vars;
}

Called from the template file with:

<?php query_posts(set_order_by_last_date(clone $wp_query));?>

The vars and eo_ functions are from the excellent event organiser plug-in. Thanks @jack-johansson for the pointers.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)