In the Admin Panel -> Settings -> Miscellenaous
The "Show subcategories" is by deffault set to false.

If you have subcategories and the default is false, when you click on a category to display all the stories under this category, it won't include it's subcategories in the search result. However, if you want to also include the subcategories of the category you are searching, you have to set the value to true.

The code to include the subcategories in the search won't work as it is.