Use gmw_update_post_location() to geocode an existing post's address or reverse-geocode its coordinates and save its default location in GEO my WP.
Requirements and integration timing
GEO my WP and its Posts Locator component must be active, and the function must be loaded before you call it. Configure the site's geocoding provider first. For Google, follow the browser and server API key instructions; this PHP operation needs working server-side geocoding.
Call the function from your integration's successful post-save callback, after its own permission, nonce and field validation checks. Pass the ID of the post that was actually saved. The function checks whether that post exists; it does not authorize the visitor, validate your form submission or create the post for you. An importer or background task must enforce its own authorization and data rules.
Signature and parameters
gmw_update_post_location( $post_id, $location, $user_id, $location_name, $force_refresh );
$post_id— integer; default0- The ID of an existing WordPress post. An absent or unknown ID stops processing.
$location— string or array; default empty array- A single address string; an array using
street,apt,city,state,zipcodeandcountry; or an array with bothlatandlng. Coordinates still go through reverse geocoding. $user_id— integer; default0- The location owner. A zero/empty value falls back to the currently signed-in user's ID, which can be
0in an anonymous or background context. It does not automatically use the post author. Pass that author's ID explicitly when that is the intended owner. $location_name— string; default empty string- An empty value uses the post title. Supplying a name labels the default location; it does not create a separate location for that post.
$force_refresh— boolean; defaultfalse- Passed to the geocoder. Use
falseto allow cached geocoded data, ortrueto request a fresh lookup. It does not bypass provider credentials or quotas.
Return value and side effects
A successful save returns the location ID. A missing post, missing input or geocoding failure can return no successful result. Check for a positive location ID before reporting success; do not treat merely calling the function as confirmation that an address was saved.
The function saves GEO my WP location data for the post, updating its default location if one exists. It does not update WordPress custom address fields, create posts or append a separate Multiple Locations entry. Use your integration's own field-save logic and the appropriate Multiple Locations API for those different tasks.
Example: save an address after a post is saved
Place this helper in your own integration plugin or an appropriate code-snippet tool. You can also use a child theme's functions.php when the integration belongs to that theme. Do not edit GEO my WP's plugin files. In an existing PHP file, omit the opening <?php tag shown below.
<?php
/**
* Save a location after an integration has validated and saved its post.
*
* @param int $post_id Existing post ID supplied by the integration.
* @param string $address Validated address supplied by the integration.
*
* @return mixed Location ID, or no successful result.
*/
function mysite_gmw_sync_post_location( $post_id, $address ) {
if ( ! function_exists( 'gmw_update_post_location' ) || ! get_post( $post_id ) ) {
return;
}
$address = sanitize_text_field( $address );
if ( '' === trim( $address ) ) {
return;
}
$owner_id = (int) get_post_field( 'post_author', $post_id );
return gmw_update_post_location( $post_id, $address, $owner_id, '', false );
}
From the integration's validated, successful post-save callback, call mysite_gmw_sync_post_location( $saved_post_id, $validated_address ). These two values must come from that callback. This helper is deliberately not attached to a generic hook: the correct hook, saved post ID, field mapping and validation depend on the form or importer you are integrating.
Address arrays and coordinates
$location = [
'street' => '285 Fulton Street',
'apt' => '',
'city' => 'New York',
'state' => 'NY',
'zipcode' => '10007',
'country' => 'USA',
];
// Use an existing post ID supplied by your validated integration.
$location_id = gmw_update_post_location( $saved_post_id, $location );
For coordinates, use [ 'lat' => 40.7128, 'lng' => -74.0060 ] as the second argument. In 4.5.5.7 and 4.5.5.8, coordinate detection requires both values to be nonempty; a numeric zero enters the address-array branch instead. For a location on the equator or prime meridian, use a resolvable address rather than relying on that coordinate-array path.
Verify the saved location
- Test on a staging site using an existing test post and a valid address.
- Confirm that the call returns a positive location ID.
- Open that post's GEO my WP location editor and check its address and coordinates.
- Run a Posts Locator search whose post type, taxonomy, status and radius include the test post.
If no location is saved, first check function availability, the post ID, empty input and the geocoding provider. If a location is saved but absent from results, follow the missing-location troubleshooting guide. Do not repeat a failing lookup on every page request.
Source and older references
The function is in plugins/posts-locator/includes/gmw-posts-locator-functions.php and delegates geocoding and saving to gmw_update_location() in includes/gmw-location-functions.php. It has been available since GEO my WP 3.0 and replaces the older gmw_pt_update_location() API. New integrations should use the five-argument signature above.
For dashboard instructions rather than PHP integration, see Add and update a post location. This reference replaces the two older gmw_update_post_location documentation pages.
