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 = [
                $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);
            $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!

1 Answer 1


I think all you'd need to do is change the WP_Query from

$catquery = new WP_Query( 'cat=' .$category_id. '&posts_per_page=100' );


$catquery = new WP_Query( 'post_type=property&posts_per_page=100' );

This assumes your new custom post type is registered as "property" (lowercase) - if you registered the new post type using 'Property' or 'properties' use that instead.

UPDATE - trying to debug why the above isn't working. Try running this code to see if the query is returning the correct results. Run just this code, none of the rest of what you originally posted. This should display an array of all the results (properties) that are returned by that query, so we can make sure that part's working correctly.

$catquery = new WP_Query( 'post_type=property&posts_per_page=100' );
  • Thanks! Then I can just get rid of the line above that where it's defining the $category_id? Commented Jun 14, 2019 at 15:19
  • Yep, you shouldn't need it if all you care about is the new custom post type. Basically you're just getting 100 posts from your new custom post type now. See the WP_Query page in the documentation for all the many options you can use to grab posts of any type/category/etc: codex.wordpress.org/Class_Reference/WP_Query
    – Michelle
    Commented Jun 14, 2019 at 15:25
  • Thank you so much! Commented Jun 14, 2019 at 15:28
  • Cool! if this works, you can mark this as the correct answer by clicking the checkbox to the left. Thanks!
    – Michelle
    Commented Jun 14, 2019 at 15:46
  • Unfortunately that didn't work. I even tried something similar I found: new WP_Query( array( 'post_type' => 'property' ) ); That didn't work either. Commented Jun 14, 2019 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.