あるサイトでは、カスタムフィールドに入力された住所を元に、記事の保存時の wp_insert_post アクションで、Geolocation APIにリクエストを行い、結果をカスタムフィールドに保存するようにしています。
ありがとうございます!
wp_insert_postを使ってIDと紐付けるのかな?
初心者には難しそうですがやってみます。
解決済みにしてましたが備忘録として。
下記のサイトのコードコピペでできました。
元サイト: http://g.azucar.jp/archives/1354
add_action('save_post','set_cf_latlng');
function geocode($address){
$req ='http://maps.google.com/maps/api/geocode/xml';
$req .='?address='.urlencode($address);
$req .='&sensor=false';
$xml = simplexml_load_file($req) or die('XML parsing error');
if ($xml->status =='OK') {
$loc[0] = (string)$xml->result->geometry->location->lat;
$loc[1] = (string)$xml->result->geometry->location->lng;
}
return $loc;
}
function set_cf_latlng($postID){
if($parent_id = wp_is_post_revision($postID))
{
$postID = $parent_id;
}
$address = get_post_meta($postID , 'address',true);
if( $address ){
$location = geocode($address);
if ( !add_post_meta($postID, '_lat', $location[0], true) ) update_post_meta($postID,'_lat', $location[0] );
if ( !add_post_meta($postID, '_lnt', $location[1], true) ) update_post_meta($postID,'_lnt', $location[1] );
}else{
delete_post_meta($postID ,'_lat');
delete_post_meta($postID ,'_lnt');
}
}