Convert ACF fields to Lat/Long and Add to Map

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 :slight_smile:
===========================================

// 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!

  • Adam Czajczyk
    • Support Gorilla

    Hi Smart Web Ninja

    I hope you're well today!

    If I correctly understand the current situation, only a small change should be necessary within the first code above. You would want to replace the category query wit query fetching post of selected type, so replace this part:

    $category_id = get_cat_ID('properties');
    $catquery = new WP_Query( 'cat=' .$category_id. '&posts_per_page=100' );
    while($catquery->have_posts()) : $catquery->the_post();

    with this

    $my_query = new WP_Query(
    	array(
    		'post_type' => 'property',
            	'posts_por_page' => 100
    		)
    	);
    while( $my_query->have_posts() ) : $my_query->the_post();

    should do the trick, I believe :slight_smile:

    Best regards,
    Adam

    • Smart Web Ninja
      • Design Lord, Child of Thor

      Thanks Adam Czajczyk !

      So, it looks like that worked as far as querying the Properties from the the "property" custom post type. But, it's not pulling and converting the coordinates for some reason.

      The problem seems to either be here:

      $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; }

      Or the geocode in functions.php.

      In the above code, it's saying that if it can't get the lat/long coordinates, to just make them 0,0 - and that's what the code is spitting out:

      Any ideas for making the address conversion to lat/long work?

      Thanks again!

  • Adam Czajczyk
    • Support Gorilla

    Hello Smart Web Ninja

    I'm sorry for the delay on our end!

    Since it's actually printing out 0 for both coordinates then it means that these two lines are not getting any data or are getting 0 values:

    $latitude = get_post_meta($post->ID, 'latitude', true);
       $longitude = get_post_meta($post->ID, 'longitude', true);

    This code basically reads custom fields named (accordingly) "latitude" and "longitude" so it's possible that those might need to be updated to different names. I mean: if you're using custom fields of names different than "latitude" and "longitude", those names should be used in these two lines instead.

    In such case, most likely the same update should be done to this part of the code:

    update_post_meta($post_id, "latitude", $latitude);
     update_post_meta($post_id, "longitude", $longitude);

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.