wp-geo-posts icon indicating copy to clipboard operation
wp-geo-posts copied to clipboard

meta_query

Open KissWebMedia opened this issue 11 years ago • 5 comments

Adding meta_query to the WP_GeoQuery class seems to break the query and no results are returned (if the lat and long values are entered):

$wp_query = new WP_GeoQuery(array( 'post_type' => array( 'locations' ), 'order' => 'DESC', 'orderby' => 'date', 'posts_per_page' => '12', 'paged' => $paged, 'meta_query' => array( array( 'key' => 'key_here', 'value' => 'yes', 'compare' => 'IN', ) ), 'latitude' => $lat, // User's Latitude (optional) 'longitude' => $lng, // User's Longitude (optional) 'radius' => 20, // Radius to select for in miles (optional) ));

KissWebMedia avatar Nov 19 '13 11:11 KissWebMedia

any progress on this?

jesperbjerke avatar Dec 21 '13 02:12 jesperbjerke

Hello, are others experiencing this problem? I've pinpointed the the issue and this seems to be when both a meta_query and radius is being used. If I take the radius away from the above query it works. So unfortunately as the meta_query is more important to us we're not able to use the radius at the moment.

Does anyone know what may be causing this?

KissWebMedia avatar Feb 08 '14 23:02 KissWebMedia

After enabling WP debug I've found out that there was an SQL error when both meta_query and the radius was enabled.

The issue seems to be with GROUP BY in the sql command. This is what breaks the query when the radius is entered. And as GROUP BY is only added when a taxonomy_query or meta_query is present the two together (meta_query and radius) are causing the problem.

I used the posts_groupby filter (http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_groupby) to remove the GROUP BY. I added the following to line 139 of geo-query.php and everything seems to be working well now:

function posts_groupby($groupby) { $groupby = false; return $groupby; }

I called the function by adding the following to line 41:

add_filter('posts_groupby', array(&$this, 'posts_groupby'), 10, 2);

KissWebMedia avatar Feb 09 '14 01:02 KissWebMedia

Thanks! I came across this same issue and the filter fixed it for me too.

Enchiridion avatar Nov 12 '15 21:11 Enchiridion

Yay! It solved my issue too! In my case, it wasn't meta_query which was used, but category ID.

Thanks a lot!

nagman avatar Aug 12 '16 08:08 nagman