I’m tweaking a wp_query quite a lot with the add_filter function.

I’m getting this error from wordpress: Unknown column 'lat' in 'field list'

The final SQL output is like this:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.*, 
( 3959 * acos(
    cos( radians(52.486243) ) 
    * cos( radians( lat ) ) 
    * cos( radians( lng ) - radians(-1.890401) ) 
    + sin( radians(52.486243) ) 
    * sin( radians( lat ) ) 
) ) 
AS distance , lat AS  latitude , lng AS longitude 
FROM wp_posts  
INNER 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 mt1 ON (wp_posts.ID = mt1.post_id) 
INNER JOIN lat_lng_post ON wp_posts.ID = lat_lng_post.post_id 
WHERE 1=1  
AND ( wp_term_relationships.term_taxonomy_id IN (2) ) 
AND wp_posts.post_type = 'event' 
AND (wp_posts.post_status = 'publish') 
AND ( (wp_postmeta.meta_key LIKE 'date_%_start-date' AND CAST(wp_postmeta.meta_value AS SIGNED) <= '20140704')
AND (mt1.meta_key LIKE 'date_%_end-date' AND CAST(mt1.meta_value AS SIGNED) >= '20140627') ) 
AND lat_lng_post.lat = lat 
AND lat_lng_post.lng = lng 
AND substr(wp_postmeta.meta_key, 1, 6) = substr(mt1.meta_key, 1, 6)  
GROUP BY wp_posts.ID 
HAVING distance <= 18 
ORDER BY distance ASC 
LIMIT 0, 10

I thought the AND lat_lng_post.lat = lat and AND lat_lng_post.lng = lng lines would have sorted the unknown column issue?

Does anyone know what’s wrong with the query?

Read more here: Unknown column ‘lat’ in ‘field list’


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , ,

Wordpress related questions and answers: