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( "=".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!
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/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!
Share Improve this question edited Jun 14, 2019 at 20:34 Joshua Davis asked Jun 14, 2019 at 15:08 Joshua DavisJoshua Davis 11 bronze badge1 Answer
Reset to default 0I think all you'd need to do is change the WP_Query from
$catquery = new WP_Query( 'cat=' .$category_id. '&posts_per_page=100' );
to
$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' );
var_dump($catquery);