# Searching within given categories/taxonomy terms only

## Solution #1 - via options (easy)

There is an option to restrict results to terms in the plugin back-end. Please check [this documentation](https://documentation.ajaxsearchpro.com/advanced-options/excluding-and-including-results/include-by-categories-or-terms).

## Solution #2 - Programatical restriction via custom code

For more advanced restrictions you can use the [asp\_query\_args](/hooks/filters/query-and-output/asp_query_args.md) filter.

```php
add_filter( 'asp_query_args', 'asp_include_only_term_ids', 2, 2 );
  
function asp_include_only_term_ids( $args, $id ) {
  /**
   * Enter the desired taxonomy=>terms here.
   * For example, if you want to search category 1 and 2, then:
   *  "category" => "1,2"      
   */      
  $include = array(
    "category" => "1,2,3,4",
    "post_tag" => "4,5,6,7"
  );
  // Allow results, that does not have connection with the taxonomies
  $allow_empty = true;
  
  // -- !! Do not change anything below this line !! --
  if ( !is_array($args['post_tax_filter']) )
    $args['post_tax_filter'] = array();
    
  foreach ($include as $tax => $term_string) {
    $terms = explode(",", $term_string);
    foreach ($terms as $tk => &$tv)
      $tv = trim($tv);
    
    $args['post_tax_filter'][] = array(
      'taxonomy'  => $tax,
      'include'   => $terms
    );
  }
  
  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/searching-within-given-categories-taxonomy-terms-only.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.
