我已經寫了一個函數,以採取一些自定義分類標準&在這種情況下(街道(職位名稱),城市,州,郵編)的郵政標題,並將它們地理編碼到緯度/長時間保存到自定義字段_ct_latlng
。自定義字段不更新後發佈WordPress
問題是它沒有發佈後保存或更新。任何幫助表示讚賞!
function ct_geocode_address($post_id) {
global $post;
if($_POST['post_type'] != 'listings')
return;
$city = wp_get_post_terms($post_id, 'city');
$city = $city[0];
$city = $city->name;
$state = wp_get_post_terms($post_id, 'state');
$state = $state[0];
$state = $state->name;
$zip = wp_get_post_terms($post_id, 'zipcode');
$zip = $zip[0];
$zip = $zip->name;
$street = get_the_title($post_id);
if($street && $city) {
global $post;
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($street.' '.$city.', '.$state.' '.$zip)."&sensor=false";
$resp = wp_remote_get($url);
if (200 == $resp['response']['code']) {
$body = $resp['body'];
$data = json_decode($body);
if($data->status=="OK"){
$latitude = $data->results[0]->geometry->location->lat;
$longitude = $data->results[0]->geometry->location->lng;
print $latitude.','.$longitude;
update_post_meta($post_id, "_ct_latlng", $latitude.','.$longitude);
}
}
}
}
add_action('save_post', 'ct_geocode_address');
試過,相同的結果與緯度/經度上更新或新的職位填補自定義字段。 – contempoinc
您是通過標準編輯屏幕還是通過wp_update_post()更新帖子? – Pippin
通過標準編輯屏幕。 – contempoinc