Note that there are some explanatory texts on larger screens.

plurals
  1. POFiltering WordPress posts by closest locations
    primarykey
    data
    text
    <p>I have WordPress posts that are displayed on a Google Map. The posts pull from a custom post field that includes the latlng value. The lat and lng are together in this value.</p> <p>The map also shows the users location with Googles GeoLocation. </p> <p>Could someone point me in the right direction to filter the posts by the 100 closest posts to the users location?</p> <p>I am filtering in other instances by query_posts meta key and thought it might work in this case too. </p> <p><strong>--UPDATE--</strong></p> <p>I am storing the lat and long values in two separate fields now. I can filter this just fine if I set predetermined values of the users lat/long like this...</p> <pre><code> $lat_plus = '40.496755'; $lat_minus = '35.496755'; $long_plus = '-85.051597'; $long_minus = '-89.3874682'; $the_query = new WP_Query(array( 'cat' =&gt; $custom_category, 'posts_per_page' =&gt; 50, 'order' =&gt; 'ASC', 'meta_query' =&gt; array( 'relation' =&gt; 'AND', array( 'key' =&gt; 'latitude', 'value' =&gt; $lat_plus, 'compare' =&gt; '&lt;', 'type' =&gt; 'NUMERIC' ), array( 'key' =&gt; 'latitude', 'value' =&gt; $lat_minus, 'compare' =&gt; '&gt;', 'type' =&gt; 'NUMERIC' ), array( 'key' =&gt; 'longitude', 'value' =&gt; $long_plus, 'compare' =&gt; '&lt;', 'type' =&gt; 'NUMERIC' ), array( 'key' =&gt; 'longitude', 'value' =&gt; $long_minus, 'compare' =&gt; '&gt;', 'type' =&gt; 'NUMERIC' ) </code></pre> <p>I am wanting to set these values based off the users current location. I am able to get these values and add and subtract by a number to create a square around the users current location with this code. </p> <pre><code> &lt;SCRIPT type="text/javascript"&gt; $(function() { if(navigator.geolocation) { var fallback = setTimeout(function() { fail('10 seconds expired'); }, 10000); navigator.geolocation.getCurrentPosition( function (pos) { clearTimeout(fallback); console.log('pos', pos); var point = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); new google.maps.Geocoder().geocode({'latLng': point}, function (res, status) { if(status == google.maps.GeocoderStatus.OK &amp;&amp; typeof res[0] !== 'undefined') { var change = '2.5'; var lat1 = pos.coords.latitude; var lat2 = parseFloat(lat1,10) + parseFloat(change,10); if(lat2) { $("._lat_more").html(lat2); } else fail('Failed to parse'); var lat3 = parseFloat(lat1,10) - parseFloat(change,10); if(lat3) { $("._lat_minus").html(lat3); } else fail('Failed to parse'); var long1 = pos.coords.longitude; var long2 = parseFloat(long1,10) + parseFloat(change,10); if(long2) { $("._long_more").html(long2); } else fail('Failed to parse'); var long3 = parseFloat(long1,10) - parseFloat(change,10); if(long3) { $("._long_minus").html(long3); } else fail('Failed to parse'); var zip = res[0].formatted_address.match(/,\s\w{2}\s(\d{5})/); } else { fail('Failed to reverse'); } }); }, function(err) { fail(err.message); } ); } else { $("._res").html('Geolocation unsupported!'); } function fail(err) { console.log('err', err); $("._res").html('Error ' + err); } }); &lt;/SCRIPT&gt; &lt;?php $lat_plus = '&lt;div class="_lat_more"&gt;&lt;/div&gt;'; $lat_minus = '&lt;div class="_lat_minus"&gt;&lt;/div&gt;'; $long_plus = '&lt;div class="_long_more"&gt;&lt;/div&gt;'; $long_minus = '&lt;div class="_long_minus"&gt;&lt;/div&gt; echo $lat_plus echo $lat_minus echo $long_plus echo $long_minus ?&gt; </code></pre> <p>The problem is trying to bring the JavaScript created values into the WordPress meta_query. I'm guessing that since my variables are basically creating a div that WordPress can't read the value of the div as a meta value. </p> <p>Could someone help point me in the right direction?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload