You can use Relevanssi to extend the search capabilities of the custom searches you build with Toolset.
Relevanssi is a popular plugin that extends the WordPress native search capabilities as well the free text search used in Toolset custom search. It allows you to search beyond just the post title and content and find texts in custom fields.
To use Relevanssi integration in Toolset custom searches, 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 regular WordPress posts and pages in its search results. To make your custom post types and custom fields searchable, you need to add them to 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. Use checkboxes to mark all custom post types you want Relevanssi to index.
Click the Save the options and then Build the index button.
You need the following two steps to include custom fields in the search:
- 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 don’t contain texts.
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 Settings → Relevanssi page.
Go to the Indexing options section of the Relevanssi Settings page, and paste the list of custom fields into the Custom fields field.
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.
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.
Use the following steps to add a free text search to your custom search:
- Edit the View where you want to have the text search.
- In the Custom Search section of the editor click the Add Search Field button.
- In the dropdown that appears, select Text search (title and content) option and click Next.
In the block inspector expand Filed settings. 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.
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. This includes ordering results by distance in a search for posts that include a Toolset Maps address field, where Relevanssi determines the ordering.
On the Text Search tab of the Toolset → Settings page you might see a spinning wheel after changing options in the Select which custom fields to include in text searches section.
If you take a look at your WordPress Debug Log you might find an error similar to the following:
PHP Fatal error: Maximum execution time of N seconds exceeded in (path to toolset)
In this case, you should increase the value of your server’s PHP max_execution_time variable. Using the value of 90 seconds solves the issue in most cases.