Search only within the current category (or any taxonomy) archive

This code will restrict the search result to the currently active taxonomy term archive.

Change the $taxonomy variable on line 3 for which taxonomies the code should apply to.

What is this, and where do I put this custom code?

add_action('asp_layout_in_form', 'asp_layout_in_form_archive_input', 10);
function asp_layout_in_form_archive_input() {
$taxonomies = 'category, post_tag, product_cat'; // Enter the taxonomy names here
// --- DO NOT CHANGE ANYTHING BELOW THIS LINE ---
$taxonomies = explode(',', $taxonomies);
foreach ( $taxonomies as $k => &$tax )
$tax = trim($tax);
if (
( in_array('category', $taxonomies) && is_category() ) ||
( in_array('post_tag', $taxonomies) && is_tag() ) ||
is_tax($taxonomies)
) {
$obj = get_queried_object();
if ( isset($obj, $obj->term_id) ) {
?>
<input type="hidden" styl="display:none;" name="asp_tax_archive" value="<?php echo $obj->term_id; ?>">
<?php
}
}
}
// --- DO NOT CHANGE ANYTHING BELOW HERE EITHER ---
add_filter( 'asp_query_args', 'asp_archive_page_category_restriction', 10, 1 );
function asp_archive_page_category_restriction($args) {
if ( isset($_POST, $_POST['options']) ) {
parse_str($_POST['options'], $so);
if ( !empty($so['asp_tax_archive']) ) {
$term = get_term($so['asp_tax_archive']);
if ( !is_wp_error($term) ) {
$args['post_tax_filter'][] = array(
'taxonomy' => $term->taxonomy,
'include' => array($term->term_id),
'exclude' => array(),
'logic' => 'AND',
'allow_empty' => false
);
}
}
}
return $args;
}