Relevanssi is a popular plugin that extends WordPress native search capabilities. It allows you to search beyond just the post title and content and find texts in custom fields.
To use Relevanssi integration with Views, you need the following plugins on your site:
- Relevanssi, Free or Premium, 3.5 or later
- Toolset Views 2.2 or later
- Toolset Types 2.2 or later
By default, Relevanssi includes only posts and pages in its search results. To make your custom post types and custom fields searchable, you need to add them to the Relevanssi’s index.
Go to the Indexing options on the Relevanssi Settings page. You will see a table that lists all post types on your site. Mark the checkbox for all custom post types you want indexed by Relevanssi.
Scroll down a bit and click the button to save and rebuild the index.
To include custom fields in the search, you need to
- Set up the custom fields using Types plugin.
- Add the fields to Relevanssi settings and rebuild its search index.
Views offers integration with Relevanssi for post fields, but not for user fields or taxonomies.
You can add the following kinds of fields to Relevanssi’s index:
- Single line
- Multiple line
This is because these types of fields normally include text. Other fields, like checkboxes and radios should not contain textual data.
When creating or editing a supported custom field with the Types plugin, you will see an Include in search option. Check it and then follow the Text Search settings link that appears beneath it.
You are taken to the Text Search tab on the Toolset->Settings page. Your custom field now appears in the list of all “indexable” fields.
Check the boxes for all the fields you want added to Relevanssi. Copy the list of custom field slugs that appears underneath it, and go to the Relevanssi->Settings page.
Go to the Indexing options section of the Relevanssi Settings page, and paste the list of custom fields into the Custom fields to index.
Finally, you can click the button that saves the indexing options and rebuilds the index.
Your post types and custom fields are now part of the Relevanssi index and are searchable.
It is important to note that the Relevanssi index and the fields that get registered work sitewide, which means that you cannot register some fields for some searches and other fields for other searches.
Of course, Views allows you to control other elements in the search, like the post types to include, or whether you want to filter by a taxonomy, date, or author.
This text search works together with Views custom search. When you set up a custom search, you will see a Text search button in the Filter Editor.
Click the Text search button and a dialog appears. It allows you to select the contents to be searched through. In our case we want the third option called Title, body and custom fields.
Click insert, and the search shortcode is inserted, and the matching query filter is also created.
Once you add a search filter that includes custom fields, the entire search operation is performed by the Relevanssi plugin, which means that Relevanssi is also responsible for providing the results order. This is why the Views sorting order options are not used.
Relevanssi is integrated with the native WordPress search results, but it will work properly with the Views WordPress Archives in case one of them is assigned to the search results page.
By default, the WordPress search results include all the public post types. However, Relevanssi only indexes posts and pages out of the box; so once you create a WordPress Archive for search results, you will see a warning, as shown in the following image.
Of course, if you decide to change the post types to be included in the search results, the warning gets updated accordingly. Just like for custom searches, Relevanssi provides the results order, so the Views sorting options are again disabled.