Support Forum
Hi,
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?
Hi,
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_field
to re-update the field map value with only latitude,longitude
. Follow this documentation https://docs.metabox.io/actions/rwmb-after-save-field/
Hi 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!
Hi,
You can try to use this code to update the field map with the format latitude,longitude
value.
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 osm
to your field map ID.
Thanks, 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?
Hi,
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_field
and re-check this issue.
Hi Long,
Thanks a lot. That fixed it!