How to automatically check/select filter values based on the archive page?
Automatically selecting taxonomy filter values, based on the current taxonomy archive page
Category archive auto-selection
For every taxonomy
For specific taxonomies only
1
add_filter('asp_pre_get_front_filters', 'asp_change_tax_filter', 10, 2);
2
function asp_change_tax_filter($filters, $type) {
3
// --- DO NOT CHANGE ANYTHING BELOW ---
4
if ( is_archive() ) {
5
$term_id = get_queried_object()->term_id;
6
if ( empty($term_id) )
7
return $filters;
8
9
$taxonomy = get_queried_object()->taxonomy;
10
foreach ($filters as $k => &$filter) {
11
$term_id = get_queried_object()->term_id;
12
if ( $type == 'taxonomy' && $filter->data['taxonomy'] == $taxonomy ) {
13
$filter->unselect();
14
$filter->select($term_id);
15
}
16
}
17
}
18
return $filters;
19
}
Copied!
1
add_filter('asp_pre_get_front_filters', 'asp_change_tax_filter', 10, 2);
2
function asp_change_tax_filter($filters, $type) {
3
$taxonomies = 'category, post_tag'; // Comma separated list of taxonomies
4
5
// --- DO NOT CHANGE ANYTHING BELOW ---
6
if ( is_archive() ) {
7
$term_id = get_queried_object()->term_id;
8
if ( empty($term_id) )
9
return $filters;
10
$taxonomies = explode(',', $taxonomies);
11
foreach ( $taxonomies as $taxonomy ) {
12
$taxonomy = trim($taxonomy);
13
foreach ($filters as $k => &$filter) {
14
if ($type == 'taxonomy' && $filter->data['taxonomy'] == $taxonomy) {
15
$filter->unselect();
16
$filter->select($term_id);
17
}
18
}
19
}
20
}
21
return $filters;
22
}
Copied!
  • $taxonomy (string) - comma separated list of taxonomy names, where you want the code to apply
Last modified 1yr ago
Copy link