Posted on

Search custom fields

Custom fields (also known as postmeta or meta fields) are a major part of WordPress the default search won’t search. That’s a big problem for people who use solutions like Advanced Custom Fields or Carbon Fields to develop sites: on sites developed with custom field plugins large parts of the post content may be stored in the custom fields. If that information is not available for searching, the search is useless.

Relevanssi comes to rescue! Relevanssi can index and search custom field content. No coding is required for the most part, only more complicated solutions will require additional code – and if you can build those, you’ll be able to handle the required coding for Relevanssi.

Compatible custom field plugins

Relevanssi works with everything that stores the data in WordPress custom fields. That includes Advanced Custom Fields, Carbon Fields, CMB2, Toolset Types, Piklist and Custom Field Suite.

How to set up custom fields

By default Relevanssi doesn’t index any custom fields. That is easy to change. Just go to the Relevanssi settings page, navigate to the Indexing tab and find this setting:

Custom field settings
Custom field settings

Here you can see the possible settings. You can choose to index all custom fields, which is the easy option. It covers everything, and may lead to garbage results, as many themes and plugins include all sorts of meta data in the custom fields, and a lot of that is not meaningful for search.

Usually indexing visible custom fields is the best option: it covers all custom fields that are visible in the user interface, on the post edit page. WordPress hides all the custom fields that have names that start with underscores from the post edit pages: that’s where the theme settings are generally hidden.

If there are just few specific custom fields you want to index, you can choose to index only some custom fields, which will open up an input field where you can list the custom fields you want to include.

Indexing some custom fields
Indexing some custom fields. “_sku” is a typical example.

Using custom fields in excerpts

If the content matching the search query is only in custom fields and not in post content, Relevanssi won’t be able to use it in excerpts. That is fortunately easy to fix. Just go to Relevanssi settings and the Excerpts and Highlights tab, and enable the “Use custom fields for excerpts” option.

Use custom fields for excerpts
Use custom fields for excerpts option.

Now Relevanssi will automatically include the custom field content for excerpts.

Advanced excerpts from custom fields

If you want more control over how the custom fields are used for excerpts, you can use the relevanssi_pre_excerpt_content and relevanssi_excerpt_content to filter the content that is used for excerpts.

These filter hooks are almost identical: they filter the post content and get the post object and the search query as parameters. The only difference is that relevanssi_pre_excerpt_content runs before the_content is applied, and relevanssi_excerpt_content runs after.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.