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 9 years, 10 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #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
    #8050

    Its 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
    #8060

    Hi 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
    #8084

    Absolutely, I’ll keep you posted on this thread.

    Stephen Harris
    #11283

    Has any progress been made on this? I too am looking to sort events by distance (the distance from the venues, anyway).

    Alex Reynolds
    #11284

    Hi Alex,

    Sorry, I should have posted an update on this. This is possible, to order events by distance you need to:

    • Set orderby to distance
    • Provide a valid proximity query in the venue_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 9 years, 11 months ago by  Stephen Harris. Reason: Corrected example
    • This reply was modified 9 years, 11 months ago by  Stephen Harris. Reason: Corrected example
    Stephen Harris
    #11297

    Hi 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 to distance. 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
    #11301

    Hi Alex,

    It appears there is a bug in that the orderby => distance cannot be used in when using tax_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
    #11304

    Aha you have solved the mystery! Thanks for helping me out! I guess I’ll find a way around taxonomy queries for now.

    Alex Reynolds
Viewing 9 posts - 1 through 9 (of 9 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.