[wp-hackers] Search Results based on Geodata

Otto otto at ottodestruct.com
Sun Jun 6 12:37:00 UTC 2010


On Sat, Jun 5, 2010 at 4:36 PM, Mike Schinkel
<mikeschinkel at newclarity.net> wrote:
> So the problem I'm getting stuck on is that every point someone might search from is potentially a different index and thus it seems that every search result would need to be generated by a very slow sequential scan of the database.  The best I can come up with is to build ad-hoc records in an table used for indexing that contains a subset of data that is near the geo point by a square grid, "index" those by the distance from my geo point, then cache the results for 24 hours from the last time someone needed them.  Has anyone got a better way?  (I'm really hoping I'm missing something very obvious here.)
>
> BTW, if there is a good way to do index results based on geo and if geo data were to be included in core then having the searches available to work with geo might be something to consider adding to core. FWIW.

If you use the geolocation plugin:
http://wordpress.org/extend/plugins/geolocation/ then the geo data is
stored as post meta with keys of geo_latitude and geo_longitude.

Searching for posts based on distance from a point would mean writing
a custom query to retrieve post id's based on distance from some
point.

Distance calculation is complex, but fortunately the work has been done:
http://jebaird.com/blog/calculating-distance-miles-latitude-and-longitude

You'd do something like the query at the bottom of that page to
produce a "distance" column in your result, then limit based on
distance < X where x is miles or some such.

Once you have the list of ID's, you can plug that back into your main
posts_query.


-Otto


More information about the wp-hackers mailing list