Metadata, Geotagging, and the REST API

WordPress.com now supports geotagging your posts. To enable the use of geotagging, find the new Location box in your Post Editor — you’ll see a “Turn On Location Tagging” button to enable it. Because of the improvements we’ve made in handling metadata, you can now geotag posts via the REST API.

Geo metadata keys are as follows:

geo_address
(required) A human-readable description string of the location name.
geo_latitude
(required) Latitude as a signed numeric value of degrees North of the Equator.
geo_longitude
(required) Longitude as a signed numeric value of degrees East of the Greenwich Meridian.
geo_public
(internal) If a post has a geolocation set, this will be true; it will be false if that information is unset.

If the address field is empty, the post will have a private location and the latitude and longitude values will be discarded.

The following code example demonstrates the array structure for creating a new post which is geotagged.

define( 'BEARER', 'YOUR_BEARER_TOKEN' );
define( 'SITE_ID', YOUR_SITE_ID );

$options = array( 'http' =>
		array(
		'ignore_errors' => true,
		'method' => 'POST',
		'header' => array (
			0 => 'authorization: Bearer ' . BEARER,
			1 => 'Content-Type: application/x-www-form-urlencoded',
			3 => 'Host: public-api.wordpress.com'
		),
		'content' => http_build_query(
			array (
				'title' => 'Hello Local',
				'content' => 'Hello. I am a new post created by the API.',
				'tags' => 'tests',
				'categories' => 'API',
				'metadata' => array(
					array(
						'operation' => 'add',
						'key' => 'geo_address',
						'value' => 'San Francisco, CA',
					),
					array(
						'operation' => 'add',
						'key' => 'geo_latitude',
						'value' => '37.7750',
					),
					array(
						'operation' => 'add',
						'key' => 'geo_longitude',
						'value' => '-122.4183',
					),
					array(
						'operation' => 'add',
						'key' => 'geo_public',
						'value' => true
					),
				),
			)
		),
	),
);

$context = stream_context_create( $options );
$response = file_get_contents(
	'https://public-api.wordpress.com/rest/v1/sites/' . SITE_ID . '/posts/new',
	false,
	$context
);
$response = json_decode( $response );

Once a post’s geo metadata has been updated, the post object that is returned to you will contain the geo data in an updated geo object, as well as in the metadata array.

stdClass Object
(
...skipped...
    [geo] => stdClass Object
        (
            [latitude] => 39.487085
            [longitude] => -101.425781
            [address] => San Francisco
        )
...skipped...
    [metadata] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 6784
                    [key] => geo_address
                    [value] => San Francisco
                )

            [1] => stdClass Object
                (
                    [id] => 6785
                    [key] => geo_latitude
                    [value] => 39.487085
                )

            [2] => stdClass Object
                (
                    [id] => 6786
                    [key] => geo_longitude
                    [value] => -101.425781
                )

            [3] => stdClass Object
                (
                    [id] => 6787
                    [key] => geo_public
                    [value] => 1
                )
        )
)