Posted on

Indexing only some visible custom fields

I am running another plugin, which uses custom fields that are visible, but I don’t want to include them in my search results. Is there a way to allow Relevanssi to continue index visible custom fields, but exclude few specific ones that I do not want indexed?

Yes: the list of custom fields is passed through the relevanssi_index_custom_fields filter hook. The filter hook gets an array of custom field names as the parameter, you can remove the ones you don’t like and return the rest.

Add this function to your theme functions.php:

add_filter('relevanssi_index_custom_fields', 'rlv_skip_custom_fields');
function rlv_skip_custom_fields($custom_fields) {
  $unwanted_fields = array('not_this_field', 'this_one_either');
  $custom_fields = array_diff($custom_fields, $unwanted_fields);
  return $custom_fields;
}

This question was originally asked on the WP.org plugin support forum.

7 comments Indexing only some visible custom fields

  1. Could you please create example using regex, I think it will be more useful.

    Also if you are using regex does the option on backend (all,visible) will be ignored?

    1. Pras, I don’t think regex is a very useful in this; I think most cases are simply a question of blocking one or two custom fields. If you have a use case where regex would be a better solution, do tell me more.

      The backend option is not ignored. It needs to be “all” or “visible” for this to make any sense; if you’re listing individual custom fields, you won’t need to block some of them, as you can simply not list them in the first place.

      1. Hi Mikko thanks for quick reply.

        If you are using flexible content by ACF as page builder with a lot of custom field option like (background position, background image, content position, block color scheme, etc). Each of this option field will saved to postmeta table.

        In my case I have block color scheme option for each ACF block (blue, orange, black, white). This option will add extra class to block container.

        When I search phrase “John Black” all the page that contain color scheme “black” will appear.

        I think most of people will save option name with phrase “content, heading, subheading, description”, so it will be nice if we have regex that only index this option name.

        Cheers

  2. Sorry, but I still don’t understand the need for regex support here. Can you show me what exactly do you mean, what kind of regex would you use? In your example, I don’t understand how the regex would help, when the easiest solution is to just list the content fields you actually want to index.

    1. Hi Mikko,

      Sorry for confusion, but can you add wild card for this filter?
      Like this:

      add_filter(‘relevanssi_index_custom_fields’, ‘rlv_skip_custom_fields’);
      function rlv_skip_custom_fields($custom_fields) {

      $unwanted_fields = array(‘content_sections_%_background_colour’);
      $custom_fields = array_diff($custom_fields, $unwanted_fields);
      return $custom_fields;

      }

      Thanks

  3. Hi Mikko,

    Nevermind I just figure it out. Instead using regex I think its faster using strpos.

    I have lot of repeater/flexible content fields, when I use backend option to include custom field (Custom fields to index:) with this wildcard content_sections_%_content,content_sections_%_background_title it always causes fatal error due to maximum memory_limit 512mb reached.

    I fixed this using relevanssi_index_custom_fields filter to exclude custom field key if its contain xxx, as below:

    add_filter(‘relevanssi_index_custom_fields’, ‘rlv_skip_custom_fields’);
    function rlv_skip_custom_fields($custom_fields) {

    foreach($custom_fields as $key => $_field){
    if ( 0 === strpos($_field, ‘_’) ||
    strpos($_field, ‘colour’) !== false ||
    strpos($_field, ‘background’) !== false
    /* add another condition here */
    ) {
    unset($custom_fields[$key]);
    }
    }

    return $custom_fields;

    }

    hope it helps someone else.

    Cheers,
    Pras

Leave a Reply

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