Support

Account

Home Forums Front-end Issues WP Query closest locations by ACF Google Map values Reply To: WP Query closest locations by ACF Google Map values

  • Yeah, a cron job and a big lookup table would be the fastest. But this would not work for calculating nearby locations for random locations. Say, the location, as reported by the browser, of the current user.

    For another project, I’m using a quick and dirty solution:

    I take the location for which I want to retrieve nearby posts and round the latitude and longitude to the nearest degree. Then I search for posts with a location that match latitude and longitude, or one degree lower.
    So, I end up with all posts in the same degree ‘square’ and three nearby ‘squares’. Then, if needed, I can perform a closest neighbour calculation on this subset of posts, and ignore those that are too far away (and are perhaps false positives).

    Perhaps to clarify, this is my argument list:

    $args = array(
    	"post_type"			=> "any",
    	"order"				=> "RAND",
    	"post__not_in"		=> array(get_the_ID()),
    	"meta_query"		=> array(
    		'relation'		=> "OR",
    		array(
    			"relation"	=> "AND",
    			array(
    				"key"	=> "location",
    				"value"	=> '"'.$law.'.',
    				"compare"	=> "LIKE"
    			),
    			array(
    				"key"	=> "location",
    				"value"	=> '"'.$low.'.',
    				"compare"	=> "LIKE"
    			),
    		),
    	)
    );