Ordering events (not venues) by distance
WordPress Event Management, Calendars & Registration › Forums › General Question › Ordering events (not venues) by distance
This topic contains 8 replies, has 3 voices, and was last updated by Alex Reynolds 10 years, 9 months ago.
-
AuthorPosts
-
November 2, 2013 at 5:37 pm #8046
Another question: how can I sort $events by distance from my centre point?
At the moment when I extend the distance parameter for my $venue_query, so that both my test events are matched, $events is sorted by post ID, not distance. If I set my centre point to Aberdeen, my Edinburgh event is displayed first since it has the lowest post ID.
I’ve tried passing
'orderby' => 'distance', 'order' => 'ASC',
to the $venue_query (inside and outside the proximity array, with no effect) as well as the $events array, again with no effect.
Please feel free to move this to the “General Question” forum as it’s a “How do I” query rather than a bug. Thanks, David.
Pixelatic
November 2, 2013 at 5:41 pm #8050Its not currently possible to sort events by distance, just venues. This is because its venues that have the the co-ordinates, not the event. So while it would be possible (with a lot of SQL tinkering), its not natively supported. I’ll consider support for this in 1.5 (the changes I think would too involved to detail there, though it would be possible to implement this feature with code in a utility plug-in / theme, that is, without editing the core plug-in).
Stephen Harris
November 2, 2013 at 8:02 pm #8060Hi Stephen, I have solved this by running a custom WP_Query while iterating through the $venues array returned by eo_get_venues(). For each $venue I’m using tax_query to display events with a matching event-venue term_id like so:
$loop = new WP_Query(array( 'post_type' => 'event', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'event-venue', 'field' => 'id', 'terms' => $venue->term_id, ), ), ));
Not optimal I know – if I have 100s of venues I’ll be running a new WP_Query for each, and I can’t see an easy way of implementing paging. I’ll live without that for now. However it does work.
It would be great if you could implement a function (or add-on) which could accomplish this more elegantly.
Pixelatic
November 5, 2013 at 11:15 am #8084Absolutely, I’ll keep you posted on this thread.
Stephen Harris
June 13, 2014 at 10:46 am #11283Has any progress been made on this? I too am looking to sort events by distance (the distance from the venues, anyway).
Alex Reynolds
June 13, 2014 at 11:05 am #11284Hi Alex,
Sorry, I should have posted an update on this. This is possible, to order events by distance you need to:
- Set
orderby
todistance
- Provide a valid
proximity
query in thevenue_query
attribute:
For example:
$events = new WP_Query( array( 'post_type' => 'event', 'orderby' => 'distance', 'venue_query' => array( 'proximity' => array( 'center' => array( 'lat' => 55.9485588, 'lng' => -3.198519 ), 'distance' => 10, 'unit' => 'miles', 'compare' => '<=' ), ), 'event_start_after' => 'today' ));
-
This reply was modified 10 years, 9 months ago by
Stephen Harris. Reason: Corrected example
-
This reply was modified 10 years, 9 months ago by
Stephen Harris. Reason: Corrected example
Stephen Harris
June 16, 2014 at 12:54 pm #11297Hi Stephen,
Thanks for getting back to me! Unfortunately, I’m still having a problem sorting events by distance… What I had in my code was already quite similar to what you just posted:
$venue_query = array( 'proximity' => array( 'center' => $userloc, 'distance' => 100, 'unit' => 'km', 'compare' => '<=' ), ); // The new WP query $my_queryn = new WP_Query(array( 'post_type' => 'event', 'orderby'=> 'distance', 'tax_query' => array( array( 'taxonomy' => 'city', 'field' => 'slug', 'terms' => $cityname, 'operator' => 'IN' ) ), 'venue_query' => $venue_query, 'event_start_after' => 'midnight', 'event_end_after' => $rightnow, 'event_end_before' => 'tomorrow 02:00', 'posts_per_page' => 200, ));
However, this returns no posts whenever I set
orderby
todistance
. When I remove'orderby' => 'distance'
, results do come up. I know that some sort of distance is being handled for sure, as when I change the distance in the venue query, the results change accordingly.Do you have any insights as to why this would be? I feel like I’m missing something…
Alex Reynolds
June 16, 2014 at 2:57 pm #11301Hi Alex,
It appears there is a bug in that the
orderby => distance
cannot be used in when usingtax_query
. This will be fixed in 1.8.0 (the beta of which is due to be released in the next few weeks).Stephen Harris
June 16, 2014 at 4:09 pm #11304Aha you have solved the mystery! Thanks for helping me out! I guess I’ll find a way around taxonomy queries for now.
Alex Reynolds
- Set
-
AuthorPosts