How can I restrict Swiftype to search certain categories only with WordPress?

To restrict your search to specified categories, you’ll need to add a filter to your search parameters.

First, find the category IDs of the categories you want to search exclusively.

Next, add a WordPress filter function to your theme’s functions.php file to modify the Swiftype search parameters:

function swiftype_search_params_filter( $params ) {
    // set the categories to allow
    $params['filters[posts][category]'] = array( 12, 34, 56 );
    return $params;
}

add_filter( 'swiftype_search_params', 'swiftype_search_params_filter' );

Of note is that the swiftype_search_params_filter function will only affect searches that are submitted through WordPress, while the autocomplete results are made directly to Swiftype’s servers from the user’s web browser via JavaScript. Therefore, in order to make the autocomplete results match the search results, you’ll need to add a swiftypeConfig object to customize the JavaScript query parameters as well.

To do this, add an action for wp_head to your theme’s functions.php to set query params with a filter for the same allowed category IDs:

 function swiftype_javascript_config() {
?>
<script type="text/javascript">
var swiftypeConfig = {
  filters: {
    posts: {
      category: [12, 34, 56]
    }
  }
};
</script>
<?php
}

add_action( 'wp_head', 'swiftype_javascript_config' );