javascript - 使用 ajax 调用 php 函数,在该函数内部调用另一个不会触发的函数
问题描述
我在前端使用 ajax 提交 wp_query filters($args) 我的 functions.php 中有一个名为 filter_function() 的函数,这就是 ajax 提交的内容。
在 filter_function() 我有另一个函数 distance_havesine() 在我的 functions.php 文件中被调用,但该函数在 filter_function() 中不起作用
例子:
function distance_haversine($lat1, $lon1, $lat2, $lon2) {
global $earth_radius;
global $delta_lat;
global $delta_lon;
$alpha = $delta_lat/2;
$beta = $delta_lon/2;
$a = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
$c = asin(min(1, sqrt($a)));
$distance = 2*$earth_radius * $c;
$distance = round($distance, 4);
return $distance;
}
function filter_function(){
if ( isset( $_POST['distance_miles'] ) ) {
$distance_miles = $_POST['distance_miles'];
} else {
$distance_miles = 50;
}
$args = array(
'orderby' => 'date',
'order' => $_POST['date'],
'post_type' => 'product',
'posts_per_page' => -1,
's' => $_POST['search'],
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
$event_lat = get_post_meta(get_the_ID(),'prod-lat',true);
$event_long = get_post_meta(get_the_ID(),'prod-lng',true);
$full_location = get_post_meta(get_the_ID(),'prod-full-address',true);
$earth_radius = 3960.00; # in miles
$lat_1 = $event_lat;
$lon_1 = $event_long;
$lat_2 = $user_lat;
$lon_2 = $user_long;
$delta_lat = $lat_2 - $lat_1;
$delta_lon = $lon_2 - $lon_1;
//$lat_1, $lon_1, $lat_2, $lon_2 all have values
$hav_distance = distance_haversine($lat_1, $lon_1, $lat_2, $lon_2);//this is not working
var_dump($hav_distance); //<------**This is outputing "float(0)"**
if ( $hav_distance <= $distance_miles ) {
?>
<li>
<a href="">
<?php if ( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail('thumbnail'); ?>
<?php } else { ?>
<img src="/place-holder.jpeg">
<?php }; ?>
</a>
</li>
<?php }
endwhile;
wp_reset_postdata();
else :
echo 'No posts found';
endif;
die();
}
解决方案
尝试这个
function filter_function(){
global $earth_radius;
global $delta_lat;
global $delta_lon;
.....