# Search only in the same category as the current post or page (single page)

&#x20;There is no back-end option for this feature, but can be easily achieved by a small custom code. A similar code for [archive pages can be found here](/miscellaneous/post-types/search-only-in-the-same-category-as-the-current-post-or-page-single-page.md).

[What is this, and where do I put this custom code?](/safe-coding-guideline.md)

```php
add_filter( 'asp_query_args', 'asp_posts_from_same_cat', 10, 2 );
function asp_posts_from_same_cat($args, $search_id) {
  $taxonomy = 'category'; // Enter the taxonomy name
  
  // Do not change anything below
  $categories = wp_get_post_terms( $args['_page_id'], $taxonomy, array('fields' => 'ids') );
  if ( !is_wp_error($categories) && count($categories) ) {
    $args['post_tax_filter'][] = array(
      'taxonomy'  => $taxonomy,    // taxonomy name
      'include'   => $categories,   // array of taxonomy term IDs to include
      'exclude'   => array(),
      'allow_empty' => false        // allow (empty) items with no connection to any of the taxonomy terms filter
    );
  }

  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-in-the-same-category-as-the-current-post-or-page-single-page.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.
