为何$_GET在functions.php文件中无法正常工作?
问题
我的URL查询字符串为:http://website.local/residences/?status=status2&type=apartments&price=null
在页面模板中使用echo $_GET['status']可以正确得到'status2',但在functions.php的AJAX加载更多文章函数中尝试获取该值时却返回false。我需要通过这个status值做分类查询,但仅在AJAX对应的PHP函数里获取失败,请问原因是什么?
相关代码
PHP 处理函数(functions.php)
function load_more_members($offset = 0, $post_type = 'project'){ $offset = 0; $post_type = 'project'; $status_array = array(); $tax_arr = array( 'relation' => 'AND', ); if(isset($_POST['offset'])) { $offset = $_POST['offset']; } if(isset($_POST['post_type'])) { $post_type = $_POST['post_type']; } if (isset($_GET['status']) && !empty($_GET['status']) && $_GET['status'] !== 'null') { $status = $_GET['status']; array_push($status_array, array( 'taxonomy' => 'status', 'field' => 'slug', 'terms' => $status, )); } if (!empty($status_array)) { array_push($tax_arr, $status_array); } $args = array( 'post_type' => 'project', 'posts_per_page' => '4', 'offset' => $offset, 'order' => 'DESC', 'order_by' => 'date', 'tax_query' => $tax_arr ); $the_query = new WP_Query( $args ); $posts_left = $offset + 4; $found_posts = $the_query->found_posts; if ( $the_query->have_posts() ) : ob_start(); while ( $the_query->have_posts() ) : $the_query->the_post(); echo get_template_part('template-parts/latest', 'loop'); endwhile; $output = ob_get_contents(); ob_end_clean(); endif; echo json_encode(array( 'html' => $output, 'offset' => $posts_left, 'found_posts' => $found_posts )); exit(); } add_action('wp_ajax_nopriv_load_more_members', 'load_more_members'); add_action('wp_ajax_load_more_members', 'load_more_members');
JS AJAX 请求代码
function load_more_posts(offset, post_type) { $.ajax({ type: "POST", url: WPURLS.ajax_url, data: { action: "load_more_members", offset: offset, post_type: post_type, }, beforeSend: function (data) { $(".load-more-" + post_type).addClass("loading"); }, success: function (response) { var obj = JSON.parse(response); $("." + post_type + "-repeat").append(obj.html); }, error: function (error) { console.log("est" + error); }, }); } $(".load-more-post").on("click tap touch", function (e) { e.preventDefault(); var this_offset = $(this).attr("data-offset"); load_more_posts(this_offset, "post"); });
原因分析与解决方案
核心原因
AJAX请求是独立的HTTP请求,和当前页面的请求完全分离:
- 页面模板能拿到
$_GET['status'],是因为它处理的是浏览器地址栏中带参数的那个GET请求; - 而你的AJAX请求是发送到
WPURLS.ajax_url(即WordPress的wp-admin/admin-ajax.php),这个请求里没有携带原页面URL中的status等GET参数,所以后端PHP的$_GET数组里自然找不到这个值。
解决方案
方案1:在AJAX请求中主动传递参数
修改JS代码,把当前页面的URL参数(比如status)加到AJAX的请求数据里:
- 先添加一个获取URL参数的工具函数:
function getUrlParam(paramName) { const urlParams = new URLSearchParams(window.location.search); return urlParams.get(paramName); }
- 修改
load_more_posts中的AJAX请求数据:
data: { action: "load_more_members", offset: offset, post_type: post_type, status: getUrlParam('status'), // 传递status参数 type: getUrlParam('type'), // 可选:传递其他需要的参数 price: getUrlParam('price') // 可选:传递其他需要的参数 },
- 最后修改PHP中的参数获取逻辑(因为AJAX是POST请求,所以改用
$_POST):
if (isset($_POST['status']) && !empty($_POST['status']) && $_POST['status'] !== 'null') { $status = $_POST['status']; array_push($status_array, array( 'taxonomy' => 'status', 'field' => 'slug', 'terms' => $status, )); }
方案2:通过来源URL解析参数(备选)
如果不想修改JS,也可以在PHP中通过wp_get_referer()获取原页面的URL,再解析其中的参数:
$referer = wp_get_referer(); if ($referer) { $referer_params = wp_parse_url($referer, PHP_URL_QUERY); parse_str($referer_params, $params); if (isset($params['status']) && !empty($params['status']) && $params['status'] !== 'null') { $status = $params['status']; // 后续逻辑不变 } }
注意:这种方式依赖浏览器发送Referer请求头,部分浏览器或隐私设置可能会屏蔽该字段,可靠性不如方案1。
内容的提问来源于stack exchange,提问作者matt.mcg
相关产品推荐
相关产品推荐

