• Hello,
    I like to create a custom query to get the nearest venues around the user.

    what I got are the user geodata, the wished radius and the venue geodata.

    user_lat
    user_lng
    user_radius

    the venue geodata are stored in custom fields of the post.

    venue_lat
    venue_lng

    I got this query from google. And it works fine with a simple database in DW (not WP).

    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
    }
    
    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    
    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
    break;
    }
    return $theValue;
    }
    }
    
    $the_rad_stores = "-1";
    if (isset($_POST["the_rad"])) {
    $the_rad_stores = $_POST["user_rad"];}
    $the_lat_stores = "-1";
    if (isset($_POST["the_lat"])) {
    $the_lat_stores = $_POST["user_lat"];
    }
    $the_lng_stores = "-1";
    if (isset($_POST["the_lng"])) {
    $the_lng_stores = $_POST["user_lng"];
    }
    mysql_select_db($database_testi, $testi);
    $query_stores = sprintf("SELECT id, address, name, venue_lat, venue_lng, ( 6371 * acos( cos( radians(%s) ) * cos( radians( venue_lat ) ) * cos( radians( venue_lng ) - radians(%s) ) + sin( radians(%s) ) * sin( radians( venue_lat ) ) ) ) AS distance FROM markers HAVING distance < %s ORDER BY distance LIMIT 0 , 20",
    GetSQLValueString($the_lat_stores, "double"),
    GetSQLValueString($the_lng_stores, "double"),
    GetSQLValueString($the_lat_stores, "double"),
    GetSQLValueString($the_rad_stores, "int"));
    $stores = mysql_query($query_stores, $testi) or die(mysql_error());
    $row_stores = mysql_fetch_assoc($stores);
    $totalRows_stores = mysql_num_rows($stores);
    ?>

    How do I create a custom query so I get the Posts and i can use the in the loop?

    Thanks for helping,
    Denis

  • The topic ‘Geo Radius Search Query’ is closed to new replies.