我想我有一个有点离经叛道的问题。我有一个函数可以向mysql数据库添加一条记录,其中包含位置lat和lng信息。但是,有时该函数工作不正常,导致无法创建db记录。因此,我想创建一个循环来不断检查函数是否创建了db记录,如果没有,请再次执行该函数。只是想说清楚;我使用的函数是一个默认的geomywp函数(reference)。有时它不能正常工作,我猜是由于谷歌Mapapi的响应时间。
我的functions.php中有以下内容,它似乎正在工作。我真的很想知道这是否是一个适当的方式来完成上述,因为我不确定。谢谢
global $wpdb;
$myccount = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM ".$wpdb->prefix."gmw_locations WHERE object_id = %d", $post_id));
if ($myccount <= 0) :
while ($myccount <= 0) :
gmw_pt_update_location( $args ); //The function I would like to check.
$myccount = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM ".$wpdb->prefix."gmw_locations WHERE object_id = %d", $post_id));
endwhile;
endif;
下面是我在为cpt创建post时用于导入地址的函数
function gmw_update_post_type_post_location( $post_id ) {
// Return if it's a post revision
if ( false !== wp_is_post_revision( $post_id ) )
return;
// check autosave //
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
//check if user can edit post
if ( !current_user_can( 'edit_post', $post_id ) )
return;
//Add post Meta
add_post_meta($post_id, 'address', $_POST['address'], false);
//get the address from the custom field "address"
$addressarray = get_post_meta( $post_id, 'address', true );
$address = $addressarray[0];
//$address = $_POST['address'];
//varify that address exists. Otherwise abort the function.
if ( empty( $address ) )
return;
//include the update location file file
include_once( GMW_PT_PATH .'/includes/gmw-pt-update-location.php' );
//make sure the file included and the function exists
if ( !function_exists( 'gmw_pt_update_location' ) )
return;
//Create the array that will pass to the function
$args = array(
'post_id' => $post_id, //Post Id of the post
'address' => $address // the address we pull from the custom field above
);
//Start counting 1
global $wpdb;
$myccount = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM ".$wpdb->prefix."gmw_locations WHERE object_id = %d", $post_id));
if ($myccount <= 0) :
while ($myccount <= 0) :
//Add location
gmw_pt_update_location( $args );
//Start counting 2
$myccount = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM ".$wpdb->prefix."gmw_locations WHERE object_id = %d", $post_id));
endwhile;
endif;
}
//execute the function whenever post type is being updated
add_action( 'save_post_mpp-gallery', 'gmw_update_post_type_post_location', 13, 1 );
将记录插入数据库的函数是:gmw\u update\u location这个函数可以在geomywp插件中找到。请参见此链接以获取一些参考:https://github.com/fitoussi/geo-my-wp/blob/master/includes/gmw-location-functions.php#lc194
暂无答案!
目前还没有任何答案,快来回答吧!