• Resolved creativ3y3

    (@creativ3y3)


    I am trying to find the nearest shop within a custom post type using Lat / Long that lives in the custom post meta. I can manage to get it to work, but with some missing posts. In particular some of the data doesn’t exist on all posts such as “address2”. If address 2 is missing it fails to bring back all the records within the 50mile criteria. How can i get it to bring back all the data even if “address2” is missing for one of the entries. Here is where it is at:

    function my_nearest_shop() {
    
        global $wpdb;
        $distance = "50 miles";
        $lat =  "37.831313606716186";
        $lng = "-122.038259135056737";
     
        $earth_radius = 3959;
    
    	$sql = $wpdb->prepare( "
    		SELECT DISTINCT
    			p.ID,
    			p.post_title,
    			latitude.meta_value as locLat,
    			longitude.meta_value as locLong,
                address.meta_value as address,
                address2.meta_value as address2,
                city.meta_value as city,
                state.meta_value as state,
                zip.meta_value as zip,
                country.meta_value as country,
                phone.meta_value as phone,
    			( %d * acos(
    			cos( radians( %s ) )
    			* cos( radians( latitude.meta_value ) )
    			* cos( radians( longitude.meta_value ) - radians( %s ) )
    			+ sin( radians( %s ) )
    			* sin( radians( latitude.meta_value ) )
    			) )
    			AS distance
    		FROM $wpdb->posts p
    		LEFT JOIN $wpdb->postmeta latitude ON p.ID = latitude.post_id
    		LEFT JOIN $wpdb->postmeta longitude ON p.ID = longitude.post_id
            LEFT JOIN $wpdb->postmeta address ON p.ID = address.post_id
            LEFT JOIN $wpdb->postmeta address2 ON p.ID = address2.post_id
            LEFT JOIN $wpdb->postmeta city ON p.ID = city.post_id
            LEFT JOIN $wpdb->postmeta state ON p.ID = state.post_id
    		LEFT JOIN $wpdb->postmeta zip ON p.ID = zip.post_id
            LEFT JOIN $wpdb->postmeta country ON p.ID = country.post_id
            LEFT JOIN $wpdb->postmeta phone ON p.ID = phone.post_id
            WHERE 1 = 1
    		AND p.post_type = 'wp_shops'
    		AND p.post_status = 'publish'
    		AND latitude.meta_key = 'wpshops_lat'
    		AND longitude.meta_key = 'wpshops_lng'
            AND address.meta_key = 'wpshops_address'
            AND address2.meta_key = 'wpshops_address2'
            AND city.meta_key = 'wpshops_city'
            AND state.meta_key = 'wpshops_state'
            AND zip.meta_key = 'wpshops_zip'
            AND country.meta_key = 'wpshops_country'
            AND phone.meta_key = 'wpshops_phone'
    		HAVING distance < %s
    		ORDER BY distance ASC",
    		$earth_radius,
    		$lat,
    		$lng,
    		$lat,
    		$distance
    );
     
    $nearbyLocations = $wpdb->get_results( $sql );
     
    if ( $nearbyLocations ) {
    return $nearbyLocations;
    }
    else{
        return "no results";
    }
    


    Any help appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘sql query’ is closed to new replies.