How to automatically check/select filter values based on the archive page?

Automatically selecting taxonomy filter values, based on the current taxonomy archive page

Where do I put this custom code?

add_filter('asp_pre_get_front_filters', 'asp_change_tax_filter', 10, 2);
function asp_change_tax_filter($filters, $type) {
  // --- DO NOT CHANGE ANYTHING BELOW ---
  if ( is_archive() ) {
      $term_id = get_queried_object()->term_id;
      if ( empty($term_id) )
          return $filters;
          
      $taxonomy = get_queried_object()->taxonomy;   
      foreach ($filters as $k => &$filter) {
          $term_id = get_queried_object()->term_id;
          if ( $type == 'taxonomy' && $filter->data['taxonomy'] == $taxonomy ) {
              $filter->unselect();
              $filter->select($term_id);
          }
      }
  }
  return $filters;
}

Last updated

Copyright Ernest Marcinko