I'm trying to get my posts to order by radius, and so far i'm struggling, I've been using the following tut by Steve MArks, http://biostall.com/performing-a-radial-search-with-wp_query-in-wordpress

Here's the code that produces the error the following error.

function location_posts_where( $where )  
    global $wpdb;
    // Specify the co-ordinates that will form  
    // the centre of our search  
    $lat = '50.12335';  
    $lng = '-1.344453';  

    $radius = 10; // (in miles)  

    // Append our radius calculation to the WHERE  
    $where .= " AND $wpdb->posts.ID IN (SELECT post_id FROM lat_lng_post WHERE 
         ( 3959 * acos( cos( radians(" . $lat . ") ) 
                        * cos( radians( lat ) ) 
                        * cos( radians( lng ) 
                        - radians(" . $lng . ") ) 
                        + sin( radians(" . $lat . ") ) 
                        * sin( radians( lat ) ) ) ) <= " . $radius . ")";  

    // Return the updated WHERE part of the query  
    return $where;  

The error that is chucked up in the debug log is:

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IN (SELECT `post_id` FROM `lat_lng_post` WHERE ( 3959 * acos( cos( rad' at line 1]
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'festival-event' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND .ID IN (SELECT `post_id` FROM `lat_lng_post` WHERE ( 3959 * acos( cos( radians(50.12335) ) * cos( radians( 'lat' ) ) * cos( radians( 'lng' ) - radians(-1.344453) ) + sin( radians(50.12335) ) * sin( radians( 'lat' ) ) ) ) <= 10) ORDER BY wp_posts.post_date DESC LIMIT 0, 10

Seems to me that it fails to connect to the database.

I've tried backticking db parts of the string like previous threads say to, but that doesn't work either.

My knowledge of SQL isn't high but it is on my to do list, any help would be really appreciated.

  • One of the users - @birgire - on here wrote the plug-in WP Geo Query, which enables you to do geo location queries with WP_Query. If it is suitable for you depends on the data structure you have, especially it works with custom fields, not a custom table. Commented Aug 25, 2015 at 12:03

2 Answers 2


Not sure about DB connection but there seems some syntax issue.

lat and lng have been used unlike PHP variables at some places in your code. I believe, they should have been $lat and $lng respectively instead.

  • the lat & lng are the table names from the database. whereas the $lat & $lng are the variables set up at the top of the function
    – Addzy
    Commented Aug 25, 2015 at 8:35

In your query, you'll see:

AND .ID IN (SELECT 'post_id' FROM 'lat_lng_post' WHERE (.

The actual posts table name is missing from the query.

The problem is you are missing the $ from your declaration of wpdb.

global $wpdb.

  • I amended the $, how silly of me, as for the posts table name, that is the 'post_id' part of the query between SELECT and FROM
    – Addzy
    Commented Aug 25, 2015 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.