【发布时间】:2015-08-24 09:51:56
【问题描述】:
我正在尝试将 wordpress 帖子中的数据插入到新表数据库表中,而不是 WP_postmeta。我的功能正在运行,它会插入$postid 完全没有问题。我遇到的问题是我需要从 Marty Spellerbergs 的“地址地理编码器”插件中插入纬度和经度坐标。
在https://wordpress.org/plugins/address-geocoder/ 可以看到的插件页面上,Marty 说您可以使用这些来访问循环内的坐标:
<?php echo get_geocode_lng( $post->ID ); ?>
<?php echo get_geocode_lat( $post->ID ); ?>
现在我知道在 functions.php 文件中,我们实际上并不在其中,所以我尝试了很多不同的方法来访问这些数据,但我就是做不到。有没有办法编辑 Marty 指定的那些行,以便在函数中调用它们?。
这是我在这方面的众多尝试之一:
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' )
【问题讨论】: