Thanks for looking into this more. I had thought maybe the distance calculation was somehow incorrect, too, but that seemed unlikely, since that would surely have been noticed long ago. So I decided to see why the wrong results were picked. I modified the query slightly so I could see the address that was being matched (the modification are the addition of the 4 fields wp_posts.ID,
wp_posts.post_title, wp_toolset_maps_address_cache.address_passed, wp_toolset_maps_address_cache.point following SELECT). The modified query is:
SELECT
wp_posts.ID,
wp_posts.post_title,
wp_toolset_maps_address_cache.address_passed,
wp_toolset_maps_address_cache.point
FROM
wp_posts
LEFT JOIN wp_term_relationships ON (
wp_posts.ID = wp_term_relationships.object_id
)
INNER JOIN wp_postmeta ON (
wp_posts.ID = wp_postmeta.post_id
)
INNER JOIN wp_postmeta AS tmapsmeta ON (wp_posts.ID = tmapsmeta.post_id)
LEFT JOIN wp_toolset_maps_address_cache ON wp_toolset_maps_address_cache.address_passed = tmapsmeta.meta_value
WHERE
1 = 1
AND (
wp_term_relationships.term_taxonomy_id IN (747)
)
AND (
wp_postmeta.meta_key = 'wpcf-address'
)
AND wp_posts.post_type = 'counselor'
AND (
(wp_posts.post_status = 'publish')
)
AND ST_Distance_Sphere(
ST_PointFromText('POINT(-95.930404 36.019042)'),
wp_toolset_maps_address_cache.point
) < 40233.6
GROUP BY
wp_posts.ID
ORDER BY
ST_Distance_Sphere(
ST_PointFromText('POINT(-95.930404 36.019042)'),
wp_toolset_maps_address_cache.point
) ASC
Note that this only changes what's being returned by the query, and doesn't change which posts are returned. Running that query, you can see the "address_passed" for each of the incorrect results is "MA, LPC", "singh", or "marsh". Also note that the points returned for those values are all close to the search point. That is, the distance check is probably correct. Those values didn't seem likely to be addresses in the address fields, so I picked the first wrong result, with an ID of 12818 (you can pick any of them). And ran the following query:
SELECT * FROM wp_postmeta WHERE post_id='12818'
Looking at the results, I could see that the only row for which meta_value is "MA, LPC" (which matches "address_passed" in the query above) is for "wpcf-suffix". The "wpcf-address" has an expected address, and there's only one for that counselor. Obviously, the query was not limiting the search for address_passed to the "wpcf-address" field as expected. That's when I reformatted the query (using hidden link) to make it easier to read. That's when I understood how the query was incorrect, and what the correct query would be. The query needs to check for meta_key = "wpcf-address" when doing the JOIN for wp_toolset_maps_address_cache; otherwise it just matches any meta_value. This can be achieved with MySQL with my suggested query, or by simply changing "wp_postmeta.meta_key" to "tmapsmeta.meta_key". Either works, but I think moving it next to the join statement just looks nicer. 🙂
I do realize that the query is built dynamically, and that it's probably not built as straight SQL, so I understand that translating the above analysis into the actual code and then into a fix is probably not as simple as my wording above might suggest. Thanks again!