Hi, I have tapped into the geonames api to pull towns and locations for my business listings. It works fine on the filter and the search however on the admin and editing the listing it resets to blank - any ideas where I might be going wrong? The filter and search can be viewed on storycinn.com. Any help here would really be appreciated. To confirm works on the search - but not on the update or add listing using ‘hivepress/v1/forms/listing_submit’ and ‘hivepress/v1/forms/listing_update’
current code for attributes which loads but wont save!
add_filter(
'hivepress/v1/meta_boxes/listing_attributes',
function( $meta_box ) {
if ( isset( $meta_box['fields']['town'] ) ) {
print_r($meta_box);
$selected_value = $meta_box['fields']['town']['selected'] ?? null;
// Add new dynamic values
$options = fetch_irish_towns_from_geonames();
// If a selected value exists, ensure it's part of the options list
if ( $selected_value && !in_array( $selected_value, $options ) ) {
// Prepend the selected value to the options array if it's not already present
array_unshift( $options, $selected_value );
}
// Update the field with new options
$meta_box['fields']['town']['options'] = $options;
}
return $meta_box;
},
1000
);
function fetch_irish_towns_from_geonames() {
$transient_key = 'irish_towns_geonames';
$towns = get_transient($transient_key);
if ($towns === false) {
$username = 'swdadmin';
$api_url = 'http://api.geonames.org/searchJSON?country=IE&q=Wexford&featureClass=P&maxRows=1000&username=' . $username;
$response = wp_remote_get($api_url);
if (is_wp_error($response)) {
return [];
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (!isset($data['geonames'])) {
return [];
}
$towns = [];
foreach ($data['geonames'] as $place) {
$towns[] = $place['name'];
}
// Cache the results for 12 hours
set_transient($transient_key, $towns, 12 * HOUR_IN_SECONDS);
}
return $towns;
}