Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to query latitude and logitud from a Google Maps field? #23

Open
aldoalfaro opened this issue Oct 6, 2022 · 3 comments
Open

How to query latitude and logitud from a Google Maps field? #23

aldoalfaro opened this issue Oct 6, 2022 · 3 comments

Comments

@aldoalfaro
Copy link

I have a Google Maps field and I would like to query by the location. My field is called "itemLocation". What should I use as the "key"? Because the following does not work:

metaQuery: {
relation: AND
metaArray: [
{
key: "itemLocation['latitude']",
compare: BETWEEN,
value: [$latitude_start, $latitude_end]
},
{
key: "itemLocation['longitude']",
compare: BETWEEN,
value: [$latitude_start, $latitude_end]
}
]
}

@GenoPeppino
Copy link

I have a Google Maps field and I would like to query by the location. My field is called "itemLocation". What should I use as the "key"? Because the following does not work:

metaQuery: { relation: AND metaArray: [ { key: "itemLocation['latitude']", compare: BETWEEN, value: [$latitude_start, $latitude_end] }, { key: "itemLocation['longitude']", compare: BETWEEN, value: [$latitude_start, $latitude_end] } ] }

Were you able to get that sorted? I haven't installed this extension yet, but that's exactly what I have in mind. Any info you could provide would be most welcome.

@aldoalfaro
Copy link
Author

No, I could not make it work this way. Instead of this, now I am copying the latitude and longitude values to another custom fields every time the post is updated.

This is the code I am using:

function update_map_on_post_updated($post_id, $post, $update) {
$map = get_post_meta($post_id, 'item_location', true);
if (!empty($map)) {
update_post_meta( $post_id, 'item_latitude', $map['lat'] );
update_post_meta( $post_id, 'item_longitude', $map['lng'] );
}
}
add_action('save_post', 'update_map_on_post_updated', 90, 3);

This way I can use the item_latitude and item_longitude fields directly in my query.

@GenoPeppino
Copy link

No, I could not make it work this way. Instead of this, now I am copying the latitude and longitude values to another custom fields every time the post is updated.

This is the code I am using:

function update_map_on_post_updated($post_id, $post, $update) { $map = get_post_meta($post_id, 'item_location', true); if (!empty($map)) { update_post_meta( $post_id, 'item_latitude', $map['lat'] ); update_post_meta( $post_id, 'item_longitude', $map['lng'] ); } } add_action('save_post', 'update_map_on_post_updated', 90, 3);

This way I can use the item_latitude and item_longitude fields directly in my query.

Thank you. I think this is what I'm going to have to do. I had considered this already, but not thoroughly. So extra thanks for providing the add action on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants