Custom Fields and Weighted Search with ElasticPress — Part 2

by

In our previous blog post, we’ve seen how it is possible to add a custom field to the Elasticsearch query while using the weighting engine. But in that solution everything was hard-coded. Now, let’s see how we can add a custom field to the Manage Search Fields & Weighting Screen, allowing the administrator to choose if a field should be searchable or not and also change its weight.

How to add a custom field to the Search Fields & Weighting Screen

Meet the ep_weighting_fields_for_post_type filter!

Search fields are stored as a WordPress option, as an array indexed by post type. The ep_weighting_configuration_for_search filter, the one we used in the other post, changes that array right before we perform the search. The filter we’ll use in this post changes the form in the Search Fields & Weighting Dashboard.

Functions hooked to the ep_weighting_fields_for_post_type filter receives two parameters:

  • $fields: All fields displayed inside a post type.
  • $post_type: The post type being displayed.

Let’s say we want to add one meta field to the page post type. We’ll have to check if $post_type, the type being displayed, is page and then make the necessary changes to the $fields array.

The $fields array contains sections, like Attributes and Taxonomies, and those sections hold the fields. Each field has a key, that will be used in the ES query, and a value, the field human-readable name.

function epio_blog_add_custom_field_ep_weighting( $fields, $post_type ) {
	if ( 'page' === $post_type ) {
		if ( empty( $fields['meta'] ) ) {
			$fields['meta'] = array(
				'label'    => 'Custom Fields',
				'children' => array(),
			);
		}

		// Change my_custom_field here to what you need.
		$key = 'meta.my_custom_field.value';

		$fields['meta']['children'][ $key ] = array(
			'key'   => $key,
			'label' => __( 'My Custom Field', 'textdomain' ),
		);
	}

	return $fields;
}
add_filter(
	'ep_weighting_fields_for_post_type',
	'epio_blog_add_custom_field_ep_weighting',
	10,
	2
);

As currently, we don’t have a section for custom fields, also known as meta fields, we have to conditionally create it. Then we just add our field to it. As we are using a filter, remember to always return $fields, even if you don’t want to change it.

After refreshing the Manage Search Fields & Weighting Screen, you will see your custom field there, like this:

Don’t forget to make your custom field searchable, clicking on the checkbox and then on the “Save Changes” button.

If you are a plugin author, that is a very nice way to make your plugin compatible with ElasticPress!

Conclusion

ElasticPress joins Elasticsearch features with the well known WordPress easiness to use. Solving the usual use cases out of the box, the plugin is also extensible, making it possible for you to give your users the ability to search for any field you want, with any weight.

Happy searching!

Is there another search case you would like us to address? Reach out to us in the GitHub repository: https://github.com/10up/ElasticPress/issues or send us a Tweet.

Looking for professionals who know ElasticPress? Reach out to one of our specialists and we’ll be in touch right away!