FacetWP does not understand MetaBox Google Maps field
- This topic has 6 replies, 2 voices, and was last updated 3 years, 3 months ago by
Arno.
-
AuthorPosts
-
July 21, 2022 at 12:51 AM #37090
Arno
ParticipantHi,
I've created a Google Maps field: https://pasteboard.co/OQauwRaZV1Mr.jpg
And a FacetWP map facet: https://pasteboard.co/Tw0tMakwGRfC.jpg
The facet should show two markers of two posts. Instead it shows a blank map. If I filter so that only one post is displayed, it shows a map with one marker but it's on the wrong location: 0,0 I think.
It seems like FacetWP does not understand the data in the MetaBox field. It expects "latitude, longitude":
https://facetwp.com/help-center/facets/facet-types/map/What's going wrong here?
July 21, 2022 at 12:22 PM #37096Long Nguyen
ModeratorHi,
By default, the data of the map fields (Google Map or OSM) is saved to the database in the following format
latitude,longitude,zoom. If you want only to use the latitude and longitude, please follow this documentation https://docs.metabox.io/fields/map/#getting-field-value.Or use the action
rwmb_after_save_fieldto re-update the field map value with onlylatitude,longitude. Follow this documentation https://docs.metabox.io/actions/rwmb-after-save-field/July 21, 2022 at 1:49 PM #37101Arno
ParticipantHi Long,
Ok, that's likely the problem then. I assumed this would work out of the box, because FacetWP is a supported plugin. What do I need to do exactly? I'm not a developer so I need a bit more guidance.
FacetWP needs to be pointed to a field that holds he correct coordinates. So I guess your first method won't work because that's an example of how to get the lat/long values from code.
The second method might be ok. If I understand correctly, I can have code in functions.php that saves the field map value to an extra field, where it only stores lat/long without the zoom? How do I do that?
Thanks!
July 23, 2022 at 8:48 AM #37139Long Nguyen
ModeratorHi,
You can try to use this code to update the field map with the format
latitude,longitudevalue.add_action( 'rwmb_osm_after_save_field', function ( $null, $field, $new, $old, $object_id ) { // Get the submitted map address and remove the "zoom" value $map = explode( ',', $_POST['osm'] ); unset( $map[2] ); $map = join( ',', $map ); // Update field map value without "zoom" update_post_meta( $object_id, 'osm', $map ); }, 10, 5 );Remember to change
osmto your field map ID.July 23, 2022 at 4:50 PM #37141Arno
ParticipantThanks, that works for me!
Now there's one more thing I'd like to change. I want to keep the original "lat, long, zoom" value in the field, and save the adjusted "lat, long" value in a different field. So I have both formats and I can pick the one I need. I created an extra text field in the same field group.
I changed the field ID in this line:
update_post_meta( $object_id, 'id_of_text_field', $map );But it's not saved. I suppose I need to do something different because it's a text field and not a map field?
July 24, 2022 at 10:22 PM #37161Long Nguyen
ModeratorHi,
It does not work because the map field is registered and saved value before the text field will be processed. Please change the hook name to
rwmb_id_of_text_field_after_save_fieldand re-check this issue.July 25, 2022 at 12:56 AM #37167Arno
ParticipantHi Long,
Thanks a lot. That fixed it!
-
AuthorPosts
- You must be logged in to reply to this topic.