/category/child-category-1/filter_xxx-yyy/ - РАБОТАЕТ
/category/child-category-1/child-category-2/filter_xxx-yyy/ — НЕ работает
/category/child-category-1/child-category-2/ child-category-3/filter_xxx-yyy/ — НЕ работает
Код: Выделить всё
add_action('pre_get_posts', array($this, 'filter_posts_by_custom_fields'));
add_action('init', array($this, 'register_rewrite_rules'));
public function register_rewrite_rules() {
add_rewrite_rule('^category/([^/]+)/(.+)/?$', 'index.php?category_name=$matches[1]&custom_filter=$matches[2]', 'top');
add_rewrite_tag('%custom_filter%', '([^&]+)');
}
public function filter_posts_by_custom_fields($query) {
if (!$query->is_main_query() || !is_category() || !$query->get('custom_filter')) return;
$custom_filter = $query->get('custom_filter');
$filters = explode('-and-', $custom_filter);
$meta_query = ['relation' => 'AND'];
foreach ($filters as $filter) {
if (strpos($filter, 'filter_') === 0) {
list($key, $value) = explode('-', $filter, 2);
$values = explode('-plus-', $value);
$meta_query[] = [
'key' => $key,
'value' => count($values) > 1 ? $values : $value,
'compare' => count($values) > 1 ? 'IN' : '='
];
}
}
$query->set('meta_query', $meta_query);
}
Извините за мой английский.
Подробнее здесь: https://stackoverflow.com/questions/791 ... s-not-work
Мобильная версия