I am having two meta keys, longitude and latitude, and what I am trying to do is the equivalent of the google store location algorithm for distance, in wp_query:

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

What I am trying to do, is get a list of custom post types (stores) that contain long/lat (custom fields), and having an x distance (it's for a store locator, like google's example).

I have already done it with $wpdb way, but I am looking for a wp_query because I want the paging and the extra filtering by using/removing an add_filter() directive to the page. Is it possible to execute such complex query, or should I stick to $wpdb method?

1 Answer 1


You can do that. There are filters available in WP_Query that enable you to modify the query just as you like. The filter posts_clauses filters all of the parts of the query together. There are also filters specific to the parts of the query too. The best way to know about them is, search posts_clauses in "wp-includes/query.php". Just 3-4 lines before this filter should be a series of filters to filter out the full query part-by-part. The filter tags are self explanatory & you can experiment with them to see which combination works out the best for you

  • I see what you mean, probably it needs more checking from my behalf. Thanks.
    – Panagiotis
    Oct 23, 2012 at 21:05

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.