Sorting Grids with Essential Grid and Events Manger

After researching this quite a bit I managed to sort the Events Manager events by date in the Essential Grid plugin.

If you started with the solution above, you can delete the added parameters field under source tab of Essential Grids, it’s not needed anymore.

So here’s what I have in functions.php. Note: I have grid 1 & 2 used for current events and sorted by ascending “event start date” and I suppose grid 99 would work for past events in descending order, but I did not test it.

/******************************************************************
 * Querry for Essential Grid to work with events
 ******************************************************************/
add_filter('essgrid_query_caching', 'eg_disable_caching', 10, 2);

function eg_disable_caching($do_cache, $grid_id){ //disable caching for the particular grid
    if($grid_id == 1 || $grid_id == 2 || $grid_id == 99){ //replace 99 with other grid id - see below
        return false;
    }
    return true;
}

add_filter('essgrid_get_posts', 'eg_modify_query', 10, 2);
//This is for future events
function eg_modify_query($query, $grid_id){
    if($grid_id == 1 || $grid_id == 2){ //replace with your grid id
        $query['meta_query'] = array( 'key' => '_start_ts', 'value' => current_time('timestamp'), 'compare' => '>=', 'type'=>'numeric' );
        $query['meta_key'] = '_start_ts';
        $query['meta_value'] = current_time('timestamp');
        $query['meta_value_num'] = current_time('timestamp');
        $query['meta_compare'] = '>=';
        $query['order'] = 'ASC';
        $query['orderby'] = 'meta_value';
    }
//This is for past events
    if($grid_id == 99){ //replace 99 with your desired grid id
        $query['meta_query'] = array( 'key' => '_start_ts', 'value' => current_time('timestamp'), 'compare' => '<=', 'type'=>'numeric' );
        $query['meta_key'] = '_start_ts';
        $query['meta_value'] = current_time('timestamp');
        $query['meta_value_num'] = current_time('timestamp');
        $query['meta_compare'] = '<=';
        $query['order'] = 'DESC';
        $query['orderby'] = 'meta_value';
    }
return $query;
}

And this is the HTML that goes in my grid to get the content of the events:

[event post_id='%post_id%']
<span style="font-weight: bold;color: #597eba;">Date:</span> #_EVENTDATES<br>
<span style="font-weight: bold;color: #597eba;">Time:</span> #_EVENTTIMES<br>
<span style="font-weight: bold;color: #597eba;">Location:</span> #_LOCATIONTOWN (#_LOCATIONSTATE)
[/event]

You can use any Events Manager shortcodes for events or locations within the [event] tags.

I hope this can be useful for someone else.