Sorting Grids with Essential Grid and Events Manger

I am using Events Manager to control my Events, but Essential Grids to display previews of these events in various places throughout my site. An example of this is http://staging-dbmax.transitiongraphics.co.uk/events/.

Essentially I am trying to get Essential Grids to recognise the Date Field of the event and sort the grid accordingly. Whilst hiding events that are in the past.

Thus far through research I have managed to isolate the grids and pull in the date, whilst hiding older events. However it doesn’t quite work. Some of the events do not place correctly in the grid.

To achieve what I have so far I did the following:

Added parameters field under source tab of Essential Grids:

( 'meta_query' => array( array( 'meta_key' => '_event_start_date' , 'meta_value' => $today, 'compare' => '>=' , 'type' => 'date')) , 'orderby' => 'meta_value' ,  ) ); ?>

Added the following to my Functions PHP file:

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 == 43 || $grid_id == 48 || $grid_id ==  50){ //replace 1 with your desired grid id
  return false;
 }
 return true;
}

add_filter('essgrid_get_posts', 'eg_modify_query', 10, 2);

function eg_modify_query($query, $grid_id){
 if($grid_id == 43 || $grid_id == 48){ //replace 1 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'] = '>=';
 }

 if($grid_id == 50){ //replace 1 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'] = '<=';
 }

 return $query;
}

And finally, this HTML goes in the skin of the Grid to pull the date in from Events Manager:

[event post_id="%post_id%"]#_EVENTDATES[/event]

Obvously if anyone has a better solution, I’d be happy to use that instead, otherwise, there something in the above which is not sorting correctly?

Solutions Collecting From Web of "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.

It didn’t work for me at first and I found out Essential Grid must have changed something. The shortcodes now need single quote instead of double quotes in the skin editor:

[event post_id='%post_id%']

Works also like this:

  1. Set a reference meta for _event_start_date (alphabetic) in Essential Grids “Meta Data Handling” menu section

  2. Put the above Code in your theme’s (or better: child theme’s) function.php

  3. Then go to your essential grid item under tab “Nav-Filter-Sort” and choose Event Start Date for sorting.