Instead of using a regular post with category (Properties), I created a Custom Post Type (Property). So, I'd like to convert the following code so that it's using the custom post type instead of category.
I'm a complete newb, so asking for some help :)
// For each property post, convert its ACF address fields to longitude/latitude coords and add to map
var geojson = [
<?php
$category_id = get_cat_ID('properties');
$catquery = new WP_Query( 'cat=' .$category_id. '&posts_per_page=100' );
while($catquery->have_posts()) : $catquery->the_post();
$latitude = get_post_meta($post->ID, 'latitude', true);
$longitude = get_post_meta($post->ID, 'longitude', true);
if ($latitude != '' ) {} else { $latitude = 0; }
if ($longitude != '' ) {} else { $longitude = 0; }
$permalink = get_permalink();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ($feat_image != '' ){} else { $feat_image = get_stylesheet_directory_uri() . "/_public/comingsoon_blank.jpg"; }
?>
{
"type": "Feature",
"geometry": {
"coordinates": ["<?php echo $longitude ?>", "<?php echo $latitude ?>"],
"type": "Point"
},
"properties": {
"title": "<?php the_field('property_address_line_1'); ?>",
"neighborhood": "<?php the_field('property_neighborhood'); ?>",
"photo": "<?php echo $feat_image ?>",
"permalink": "<?php echo $permalink ?>",
"marker-color": "#000000",
"marker-size": "large"
}
},
The regular posts with category (Properties) had ACF fields for property_address_line_1 and property_address_line_2. I copied them exactly so that the Custom Post Type (Property) also has ACF fields for property_address_line_1 and property_address_line_2.
In my functions.php file is the following code:
function geocode_address($post_id)
{
$resp = wp_remote_get( "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode( get_field('property_address_line_1') . ' ' . get_field('property_address_line_2') )."&sensor=false" );
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;
update_post_meta($post_id, "latitude", $latitude);
update_post_meta($post_id, "longitude", $longitude);
}
}
}
add_action('save_post', 'geocode_address');
Everything works fine for the regular post with category. I just want it to now work with a Custom Post Type that I created instead (convert the addresses to lat/long and display them on a map).
Thanks in advance!