WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 0 down vote accepted

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

share|improve this answer
I see what you mean, probably it needs more checking from my behalf. Thanks. – Panagiotis Oct 23 '12 at 21:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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