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.

20 comments Search custom fields

    1. Sure, Meta Box is fine, as long as the data is stored in the actual WP custom fields. More complicated data structures (well, anything beyond strings) may require additional code to make Relevanssi understand the structure.

  1. Can Relevanssi be configured to create a search structure that can access multiple custom fields with an AND effect? We have a kind of page, for instance, which has antique prints; custom fields on these pages include “publisher/editor” and “publication date”. We’d like to be able to enable searches across these two fields, returning only results that contain both search values. An example might be “Curtis” in the publisher editor field, and “1862” in the publication date field, with the returns being only Curtis publications from 1862.

    R.

    1. Yes, but you don’t actually want to use Relevanssi for that kind of searching. If all searching just targets custom fields, forget Relevanssi, and just use the Meta Query functionality in WP_Query. Using Meta Query, you’ll be able to formulate complicated AND queries with multiple custom fields.

      1. Thanks.

        We won’t be using Relevanssi just for these custom fields, but for general search on a fairly substantive site. However, the primary content is held in two kinds of pages (custom post types), both of which include multiple custom fields. We want users to be able to choose what they want to search (everything; everything within one of the custom post types; custom post text content; one or more custom fields; etc.).

        1. Yes, you can run meta queries as part of Relevanssi queries – but it’s still a good idea to drop Relevanssi off and just use the meta_query, if that’s all you need in a particular search. The search will perform better that way.

          But yes, this can be done with Relevanssi, but it won’t happen out of the box, it’ll require quite a bit of custom programming to make everything work the way you want it to work.

          What does “fairly substantive” mean? Hundreds of posts? Thousands, tens of thousands, hundreds of thousands?

  2. The site is a bit unusual, in that it is not at all “interactive”. All content is posted by admins and editors, and there is no provision for comments or dialogue. Most content is held on one custom post type, with a combination of image, text, links to other images in slideshow form and links to other site pages. There are about 2000 of these and related pages, which will grow significantly over time.

    There are also some 7000 media items, all photographs, which hold content in descriptions and captions that we need to be able to include in the search.

    So in terms of “posts” it’s smallish, but is well into mid-range in terms of total content.

    On a separate note, I’ve not received any notifications by email of replies. Is this normal?

    R.

    1. Sounds like something Relevanssi can handle, but it will probably require some custom programming to make it work.

      I don’t know about the emails, that’s a Jetpack service, and I have no knowledge of that. I know you need to confirm the subscription, so if you haven’t done that, then it won’t work.

  3. One last question — you say Relevanssi could handle the task, but may require some custom programming.

    Can you confirm that Relevanssi can search media items, in the media library — that is, the caption and fields associated with photographs?

    Sorry to post this here, not really about custom fields, but this should be our last query.

    1. You do need to confirm the subscription to get the emails. Maybe the confirmation email went in your spam folder?

      Yes, Relevanssi can search media items, that doesn’t require any custom programming. That’s a core feature for Relevanssi.

  4. Hi Saari,

    I was using custom type on my website but when I searching for custom type article don’t have any result show on my site.

    I’ve done the setting:
    Indexing:
    1.Tick the custom type for the index on “Post types”
    2.Custom field: All
    3.Excerpts: Tick Index the post excerpt Index the post excerpt

    Excerpts and highlights:
    1. Use custom fields for excerpts: Tick Use custom field content for building excerpts

    Is any setting I lost?

    Best regards,

    Hsin-Hui

    1. Hsin-Hui, the settings are correct. Have you rebuilt the index after adjusting the custom field settings? Can you find the posts by title or content? If not, make sure the search is not being restricted by post type; some themes restrict the search to just posts.

  5. I have a custom field on my post called “keyword” that I would like to store a likely search term that doesn’t show up in the post anywhere. I have set Relevanssi to index all of my custom fields.

    When I test and type in my exact text that I have in the custom field (for example: tuesday blues 3) I get results but not the particular post that has that exact text in a custom field. I have reindexed after adding the custom field and its data.

    Can you help?

    Thanks! John

    1. John, at least the search on your website is not Relevanssi: it does not respond to Relevanssi parameters at all. So that might be the explanation here: something else is interfering with the search and you’re not seeing Relevanssi results at all.

      1. Well that’s a surprise! I do have FacetWP but have the integration plugin installed and activated. Is there something else that I need to do?

        1. Your theme may not be compatible, and may require changes. I’m not really up to what’s going on in the Genesis / StudioPress world these days; Genesis-based themes used to be fairly compatible with Relevanssi, but I can’t tell if that has changed in recent years. So unfortunately I can’t tell what’s up – that would require looking under the hood to see what your theme is actually doing on the search results page.

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.