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.
1
add_action('asp_layout_in_form', 'asp_layout_in_form_archive_input', 10);
2
function asp_layout_in_form_archive_input() {
3
$taxonomies = 'category, post_tag, product_cat'; // Enter the taxonomy names here
4
5
// --- DO NOT CHANGE ANYTHING BELOW THIS LINE ---
6
$taxonomies = explode(',', $taxonomies);
7
foreach ( $taxonomies as $k => &$tax )
8
$tax = trim($tax);
9
if (
10
( in_array('category', $taxonomies) && is_category() ) ||
11
( in_array('post_tag', $taxonomies) && is_tag() ) ||
12
is_tax($taxonomies)
13
) {
14
$obj = get_queried_object();
15
if ( isset($obj, $obj->term_id) ) {
16
?>
17
<input type="hidden" styl="display:none;" name="asp_tax_archive" value="<?php echo $obj->term_id; ?>">
18
<?php
19
}
20
}
21
}
22
// --- DO NOT CHANGE ANYTHING BELOW HERE EITHER ---
23
add_filter( 'asp_query_args', 'asp_archive_page_category_restriction', 10, 1 );
24
function asp_archive_page_category_restriction($args) {
25
if ( isset($_POST, $_POST['options']) ) {
26
parse_str($_POST['options'], $so);
27
if ( !empty($so['asp_tax_archive']) ) {
28
$term = get_term($so['asp_tax_archive']);
29
if ( !is_wp_error($term) ) {
30
$args['post_tax_filter'][] = array(
31
'taxonomy' => $term->taxonomy,
32
'include' => array($term->term_id),
33
'exclude' => array(),
34
'logic' => 'AND',
35
'allow_empty' => false
36
);
37
}
38
}
39
}
40
return $args;
41
}
Copied!
Last modified 1yr ago
Copy link