# 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?](/safe-coding-guideline.md)

```php
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;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://knowledgebase.ajaxsearchpro.com/miscellaneous/post-types/search-only-within-the-current-category-or-any-taxonomy-archive.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
