I'm trying to insert data from a wordpress post into a new table database table rather than WP_postmeta
. My function is working, It'll insert the $postid
no problem at all. The issues I'm having is I need to inser the latitude and longitude coordinates from Marty Spellerbergs 'Address Geocoder' Plugin.
On the plugin page, Marty says you can access the cooridnates inside the loop using these:
<?php echo get_geocode_lng( $post->ID ); ?>
<?php echo get_geocode_lat( $post->ID ); ?>
Now I know being inside the functions.php file, we are not actually inside the lood, so I've tried lots of different ways of accessing this data but I just can't do it. Is there a way to edit those lines specified by Marty so they can be called in the functions?.
This is one of my many many attempts at this:
function save_lat_lng( $post_id )
{
global $wpdb;
global $post;
$custom_lat = $_POST[get_geocode_lat( $post->ID )];
$custom_lng = $_POST[get_geocode_lng( $post->ID )];
// Check that we are editing the right post type
if ( 'festival-event' != $_POST['post_type'] )
{
return;
}
// Check if we have a lat/lng stored for this property already
$check_link = $wpdb->get_row("SELECT * FROM lat_lng_post WHERE post_id = '" . $post_id . "'");
if ($check_link != null)
{
// We already have a lat lng for this post. Update row
$wpdb->update(
'lat_lng_post',
array(
"lat" => $custom_lat,
"lng" => $custom_lng
),
array( 'post_id' => $post_id ),
array(
'%f',
'%f'
)
);
}
else
{
// We do not already have a lat lng for this post. Insert row
$wpdb->insert(
'lat_lng_post',
array(
'post_id' => $post_id,
"lat" => $custom_lat,
"lng" => $custom_lng
),
array(
'%d',
'%f',
'%f'
)
);
}
}
add_action( 'save_post', 'save_lat_lng' )
$post->ID
withget_the_ID()
.