我设法解决了这个问题。他是供进一步参考的解决方案。
1) 我现在只有1个查询变量:
function return_vars( $vars ) {
$vars[] = \'filters\';
return $vars;
}
2)我的重写规则更改为:
add_rewrite_rule( \'my-base/filters/([^\\/]*)/([^&]*)/page/([0-9]{1,})/?\', \'index.php?post_type=$matches[1]&filters=$matches[2]&paged=$matches[3]\', \'top\');
add_rewrite_rule( \'my-base/filters/([^\\/]*)/([^&]*)/?\', \'index.php?post_type=$matches[1]&filters=$matches[2]\', \'top\');
规则1:句柄分页规则2:仅处理主页面
他们必须按这个顺序。
3) 然后,我或多或少地以这种方式过滤过滤器字符串
function my_pre_get_posts( $query ) {
$filters = get_query_var(\'filters\');
// if no filter or if filters are odd, then return
if(!$filters || count(explode("/", $k4w_filters)) % 2 != 0) return;
// this is a global array with my 2 tax slugs in it
global $CUSTOM_TAXES_S;
$chunks = array_chunk(explode(\'/\', $filters), 2);
$filter_data = array_combine(array_column($chunks, 0), array_column($chunks, 1));
// filter data, populate tax_data and field_data for queries
foreach($filter_data as $key => $value) {
if(in_array($key, $CUSTOM_TAXES_S)) {
$tax_data[$key] = $value;
} else {
$field_data[$key] = $value;
}
$query->set(\'debug_tax_data\', $tax_data);
$query->set(\'debug_field_data\', $field_data);
}
// if tax_data exist set tax_query
if($tax_data) {
foreach( $tax_data as $taxonomy => $slugs ) {
// append tax query
$tax_query[] = array(
\'taxonomy\' => get_tax_name($taxonomy), // that\'s a simple function that retrieves the taxonomy name from its slug
\'field\' => \'slug\',
\'terms\' => explode(\',\', $slugs),
);
}
$query->set(\'tax_query\', $tax_query);
}
// if field_data exist set meta_query
if($field_data) {
foreach( $field_data as $key => $value ) {
// append tax query
$meta_query[] = array(
\'key\' => $key,
\'value\' => explode(\',\',$value),
\'compare\' => \'IN\',
);
}
$query->set(\'meta_query\', $meta_query);
}
}
add_action(\'pre_get_posts\', \'my_pre_get_posts\', 10, 1);
通过这种方式,我甚至可以为税收或字段提供多个术语,例如:
/my-base/filters/my-cpt/tax1/term1,term2/tax2/term1,term2/field1/val1,val2/goingon...
它会起作用的!也使用分页。
显然,上面的代码是可行的,但也有一些检查可以避免问题和黑客攻击(例如检查提供的字段名是否存在,或者是否在可过滤字段列表中等等)。
希望这对某人有所帮助:)