0

I have a custom post type for "Events" and each event can have a variable amount of "Locations" (lat/lng) which is stored as meta. I need to be able to get "Event" posts based on the bounds of a map.

I have full control over how I store the meta data of the locations for the post but I can't think of a way to create a meta_query to check against each location within an event to see if it fits within the min/max lat/lng.

Here's an example of what would work for a single location:

$args = array(
    'post_type' => 'event',
    'posts_per_page' => -1,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'coords_0_lat',
            'value' => array($lat_min, $lat_max),
            'compare' => 'BETWEEN',
            'type' => 'DECIMAL(10,7)'
        ),
        array(
            'key' => 'coords_0_lng',
            'value' => array($lng_min, $lng_max),
            'compare' => 'BETWEEN',
            'type' => 'DECIMAL(10,7)'
        )
    )
);
$query = new WP_Query($args);

But since events can have any amount of locations I can't really just do coords_0..., coords_1..., coords_2..., etc.

I have found some information on filtering the "posts_where" to change the "where" into a "LIKE" rather than an "=" but from what I understand I don't think this would work in my case as it wouldn't keep the lat/lng of a location together. (Infromation here under "4. Sub custom field values")

4
  • This isn't possible with WP_Query by itself. You'll need some SQL. I've done something similar recently, but I'm not sure if it's the same as what you're doing. How are you determining what the range is? Are you querying for events within X miles/kilometres of a point? Or something else? Jun 28, 2019 at 1:18
  • @JacobPeattie Ah, I was hoping that wouldn't be the answer... To put it simply, I will have a Google Map that will feed the map bounds to an API endpoint with the above query. I will get the minimum/maximum latitude and longitude from the map and return only the events that have locations within those bounds. I hope that makes sense.
    – Mitchell
    Jun 28, 2019 at 1:35
  • How are the bounds defined? If it's just a circle range around a point, then it will be much easier. Jun 28, 2019 at 1:44
  • @JacobPeattie It's just the border of the actual google map. I'll get the lat/lng of the north-east corner and the lat/lng of the south-west corner.
    – Mitchell
    Jun 28, 2019 at 1:52

1 Answer 1

0

I ended up having to rework how I did things. Instead of saving the locations as meta data, I created a new custom post type for locations with each location having a lat/lng and the ID of the Event saved to it. So now I have the ability to query all the locations and build an array of Event IDs from that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.