Yeah, a cron job and a big lookup table would be the fastest. But this would not work for calculating nearby locations for random locations. Say, the location, as reported by the browser, of the current user.
For another project, I’m using a quick and dirty solution:
I take the location for which I want to retrieve nearby posts and round the latitude and longitude to the nearest degree. Then I search for posts with a location that match latitude and longitude, or one degree lower.
So, I end up with all posts in the same degree ‘square’ and three nearby ‘squares’. Then, if needed, I can perform a closest neighbour calculation on this subset of posts, and ignore those that are too far away (and are perhaps false positives).
Perhaps to clarify, this is my argument list:
$args = array(
"post_type" => "any",
"order" => "RAND",
"post__not_in" => array(get_the_ID()),
"meta_query" => array(
'relation' => "OR",
array(
"relation" => "AND",
array(
"key" => "location",
"value" => '"'.$law.'.',
"compare" => "LIKE"
),
array(
"key" => "location",
"value" => '"'.$low.'.',
"compare" => "LIKE"
),
),
)
);
Also doesn’t work for me.
Thanks @frabiacca. Indeed, that’s an alternative I can think of, though it’s not quite ideal.
The big advantage that this solution can properly make use of MySQL’s geospatial functions.
There’s also this:
https://wordpress.stackexchange.com/a/183503/43252
How do you query your individually stored coordinates? Just by exact value? Or also to, in some way, find nearby locations?
Adding another response to subscribe to updates.
Because the latitude and longitude are stored in a serialised array, there is no direct way of utilising MySQL spatial functions.
Is there a solution out there that somehow facilitates exactly this?
This works, but has the downside that you have to go through all locations. If you have thousands of locations, this could be a significant resource-hog.
Thanks @virtuallast, that was what I was looking for.
I’m late to the party, but I arrived here after looking for an answer I knew existed.
The answer can be found here.
<?php
// find date time now
$date_now = date('Y-m-d H:i:s');
// query events
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'event',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '<=',
'value' => $date_now,
'type' => 'DATETIME'
),
array(
'key' => 'end_date',
'compare' => '>=',
'value' => $date_now,
'type' => 'DATETIME'
)
),
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'start_date',
'meta_type' => 'DATE'
));
if( $posts ): ?>
<h2>Events on right now</h2>
<ul id="events">
<?php foreach( $posts as $p ): ?>
<li>
<strong><?php echo $p->post_title; ?></strong>: <?php the_field('start_date', $p->ID); ?> - <?php the_field('end_date', $p->ID); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Some good support from ACF later, I learned I have to use the field key, not the field name, to update it. The field key can be shown on the ACF admin pages by tapping ‘Screen options’ at the top of the window, then turning on the relevant options.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.