Navigation überspringen

Searching Texts in Custom Fields with Toolset and Relevanssi in WordPress

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.

What to install

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 Blocks
  • Toolset Types

Adding Your Content Types to Relevanssi’s Search Index

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.

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

Click the Save the options and then Build the index button.

Building the search index

You need the following two steps to include custom fields in the search:

  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 don’t contain texts.

Registering Custom Fields

When creating or editing a supported custom field with the Types plugin, you will see the Include in search option. Select it and then save the custom field group. Finally, expand the field options again and follow the Text Search settings link beneath it.

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”

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.

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 field.

Finally, you can click the button that saves the indexing options and rebuilds the index.

Adding the fields and rebuilding the Index

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

Relevanssi Search Index Works Globally

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

Use the following steps to add a free text search to your custom search:

  1. Edit the View where you want to have the text search.
  2. In the Custom Search section of the editor click the Add Search Field button.
  3. In the dropdown that appears, select Text search (title and content) option and click Next.
Add Search Field button

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.

Selecting contents to search through

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.

Known issues

On the Text Search tab of the ToolsetSettings 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.

Updated
März 25, 2021