If you have ever wanted to add faceted search to your WordPress site without a third party plugin, it’s actually easier than you think. You can create a search form like this without too much trouble:

Search form

With the form above, if any of the boxes are checked then WordPress will limit the search to those post types. If the boxes are left unchecked then the search scope will be site wide (default). This is the code you can add to modify your search form like I’ve done here:

Faceted search form markup

The code is basically adding three checkbox inputs, each with a name of post_type[] and value of the post type slug/name, such as book. This can easily be modified to create different facets as well, such as author, category, or custom taxonomy term. A full list of possibilities can be found in the “Public” section of the List of Query Vars in the WordPress Codex.

As far as I know post_type is the only public query var that accepts an array. This means that although you can create a faceted search for different categories (for example), you can only limit the search to one category at a time. So you can search within “Category 1” OR “Category 2”, but not both at the same time. For that use case it makes more sense to set up radio buttons instead of checkboxes.

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *