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.

Required plugins

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

Adding Your Content Types to Relevanssi’s Search Index

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.

Including custom posts in the search

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.

Marking your custom post types as “indexable” in Relevanssi’s settings
Marking your custom post types as “indexable” in Relevanssi’s settings

 

Scroll down a bit and click the button to save and rebuild the index.

Including custom fields in the search

To include custom fields in the search, you need to

  1. Set up the custom fields using Types plugin.
  2. Add the fields to Relevanssi settings and rebuild its search index.

Which custom fields can I add to Relevanssi’s index with Toolset?

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
  • WYSIWYG

This is because these types of fields normally include text. Other fields, like checkboxes and radios should not contain textual data.

Registering Custom Fields

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.

Option for including the custom field in the search
Option for including the custom field in the search

 

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.

List of your custom fields that are “indexable”
List of your custom fields that are “indexable”

 

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.

Adding the Fields and Rebuilding the Index

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.

Adding fields to index using Relevanssi and Toolset

Your post types and custom fields are now part of the Relevanssi index and are searchable.

Relevanssi Search Index Works Globally

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.

Integration with custom searches

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.

Text search button in the Filter Editor section on the Views editing page
Text search button in the Filter Editor section on the Views editing page

 

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.

Selecting contents to search through
Selecting contents to search through

 

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.

Integration With The Native Search Results

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.

Warning displayed with WordPress Archives when using Toolset and Relevanssi

 

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.

Updated warning according to the search when using Relevanssi and Toolset