Can I make the Swiftype WordPress plugin search only pages?


#1

It seems like your plugin searches posts and pages. I only want it to search pages. How do I do that?

The Swiftype search plugin will index all post types that are marked as available for search (additionally, items in the post type must have a post_status of publish).

If you’d like to limit the results to just pages, you can do that by adding a filter on the object_type field of the posts DocumentType:

function swiftype_search_params_filter( $params ) {
    $params['filters[posts][object_type]'] = array( 'page' );

    return $params;
}

add_filter( 'swiftype_search_params', 'swiftype_search_params_filter', 8, 1 );

Additionally, you’ll want to add a swiftypeConfig option for the autocomplete search so that it only includes posts:

function swiftype_javascript_config() {
?>
<script type="text/javascript">
var swiftypeConfig = {
  filters: {
    posts: {
      object_type: ['page']
    }
  }
};
</script>
<?php
}

add_action('wp_head', 'swiftype_javascript_config');

For more details, see our Customizing WordPress Search tutorial as well as the companion thread: