正如@Milo精辟地指出的,Wordpress中没有像全局查询对象这样的东西。
所以我通过JS解决了这个问题,如下所示:
var isFiltered = false;
var filters;
$(load_more_button).click(function(e) {
load_more_handler(e);
});
$(filters_form).submit(function(e) {
filter_handler(e);
});
function filter_handler(e) {
isFiltered = true;
filters = $(this).serialize();
var data = {
action: \'ajax_filter\',
query: php_vars.query, // // query is got from wp_localize_script()
filters: filters
};
$.post(php_vars.adminAjaxUrl, data, function(res) {
[etc]
};
function load_more_handler(e) {
var data = {
action: \'ajax_load_more\',
query: php_vars.query, // query is got from wp_localize_script()
page: page,
isFiltered: isFiltered,
filters: filters
};
$.post(php_vars.adminAjaxUrl, data, function(res) {
[etc]
};
和在函数中。php类似:
function ajax_load_more_posts() {
$args = isset( $_POST[\'query\'] ) ? array_map( \'esc_attr\', $_POST[\'query\'] ) : array();
if( isset($_POST[\'isFiltered\']) && ( $_POST[\'isFiltered\'] ) ) {
$filters = $_POST[\'filters\'];
$args = wp_parse_args(get_ajax_filters($filters), $args); // get_ajax_filters() is a custom utility function of mine to combine filters passed by Ajax and $args
};
[loop]
wp_die();
} ;
add_action( \'wp_ajax_ajax_load_more\', \'ajax_load_more_posts\' );
add_action( \'wp_ajax_nopriv_ajax_load_more\', \'ajax_load_more_posts\' );