I’m having issues with eo_get_events when I try to limit events to a given distance from a centre point. Instead I’m getting all events returned irrespective of distance.
I have lifted code from http://codex.wp-event-organiser.com/function-eo_remote_geocode.html and made one small change; my centre is Edinburgh Castle
$venue_query = array(
'proximity' => array(
'center' => eo_remote_geocode( "Edinburgh Castle" ),
'distance' => 10,
'unit' => 'miles',
'compare' => '<='
),
);
$events = eo_get_events( array(
'event_start_after' => 'now',
'venue_query' => $venue_query,
));
I have a couple of test events in place – one in Edinburgh, one in Aberdeen. However the $events array contains both events where it should only contain my Edinburgh event. eo_remote_geocode() seems to be working OK:
print_r ( eo_remote_geocode( "Edinburgh Castle" ) )
shows
Array ( [lat] => 55.9482845 [lng] => -3.2007724 )
The event pages for both events correctly maps them where I expect; i.e. the location lat/lon data is correct.
My code is placed within a page-{slug}.php template rather than archive-event.php; could this be causing $venue_query to be overridden by the main query?
Pixelatic
BTW I’m using WordPress 3.6 with Event Organiser 2.4 and Event Organiser Pro 1.4.2
Pixelatic
Hi Pixelatic,
I’ve not been able to replicate this, but the following may help check what’s going on. It’ll print the venue query part of the raw SQL:
$venue_query = array(
'proximity' => array(
'center' => eo_remote_geocode( "Edinburgh Castle" ),
'distance' => 10,
'unit' => 'miles',
'compare' => '<='
),
);
function my_debug_sql( $pieces ){
echo '<pre>';
print_r($pieces);
echo '</pre>';
return $pieces;
}
add_filter( 'terms_clauses', 'my_debug_sql', 10 );
$events = eo_get_events( array(
'event_start_after' => 'now',
'posts_per_page' => 10,
'venue_query' => $venue_query,
));
remove_filter( 'terms_clauses', 'my_debug_sql', 10 );
Feel free to post the SQL here if it you’re not sure if it looks right… (if there is no SQL, then that suggests the venues are being incorrectly cached – but an up date in 1.3 means that this shouldn’t happen).
There’s nothing in WordPress that should be over-riding / altering the query, and the code as above should work fine in a page template. However, I’d suggest temporarily disabling other plug-ins / themes, just to rule out a conflict.
Stephen Harris
Hi Stephen, thanks for this – now traced to a bug in my template code; instead of
'venue_query' => $venue_query
in my eo_get_events() array I had
'meta_query' => $venue_query
I assume eo_get_events() was simply ignoring this and returning all event venues instead of those contained in the $venue_query array.
One final question: how can I display each event’s distance from the centre point when iterating through the $events array?
Pixelatic
Glad that’s sorted :).
Strictly speaking its the venue that has a distance from the specified location (events are returned if their venue lives within the specified radius). What you can do use the following function which takes event ID and a lat/lng co-ordinate and returns the distance:
/**
* Returns the distance of an event from a specified point
*
* @param int $event_id ID of the event
* @param array $latlng Array indexed by 'lat' and 'lng' with co-ordinate values
* @return float|bool Distance (in miles) from the specified point. False if the event has no venue.
*/
function pixelatic_distance_of_event_from( $event_id, $latlng = array() ){
$event_id = ( $event_id ? $event_id : get_the_ID() );
$venue = eo_get_venue( $event_id );
if( !$venue )
return false;
$latlng_2 = eo_get_venue_latlng( $venue );
if( empty( $latlng ) || empty( $latlng_2 ) )
return false;
$radius = 3959; // Radius of Earth in miles
//Convert to radius
$lat_from = deg2rad( $latlng['lat'] );
$lng_from = deg2rad( $latlng['lng'] );
$lat_to = deg2rad( $latlng_2['lat'] );
$lng_to = deg2rad( $latlng_2['lng'] );
//Haversine formula: http://en.wikipedia.org/wiki/Haversine_formula
$d_lat = $lat_to - $lat_from; //Difference in latitude
$d_lng = $lng_to - $lng_from; //Difference in longtitude
$sine_angle = sqrt( pow( sin( $d_lat / 2), 2) + cos( $lat_from ) * cos( $lat_to ) * pow( sin( $d_lng / 2), 2 ) );
return 2 * asin( $sine_angle ) * $radius;
}
Stephen Harris
Stephen Harris