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 which can be seen here https://wordpress.org/plugins/address-geocoder/ , 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' )
if you are passing post_id
as parameter to function save_lat_lng i think that you should change that two lines:
$custom_lat = $_POST[get_geocode_lat( $post->ID )];
$custom_lng = $_POST[get_geocode_lng( $post->ID )];
to
$custom_lat = get_geocode_lat( $post_id );
$custom_lng = get_geocode_lng( $post_id );
to access that parameters.
also if you want to check post type you should change
if ( 'festival-event' != $_POST['post_type'] )
to
if ( 'festival-event' != get_post_type($post_id) )
docs below:
https://codex.wordpress.org/Function_Reference/get_post_type
PS. next time you should paste link for plugin, it saves time :)
--- UPDATE
Coordinates were not available - problem was related to save_post hook priority value.
From plugin code i see that function updating post meta have that priority:
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
so to run your hook after that, you should set something like that:
add_action( 'save_post', 'save_lat_lng', 100 );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.