Skip to content

Instantly share code, notes, and snippets.

@tott
Created September 5, 2019 09:49
Show Gist options
  • Save tott/9d061c750f0e80703390a1f5571cf7e3 to your computer and use it in GitHub Desktop.
Save tott/9d061c750f0e80703390a1f5571cf7e3 to your computer and use it in GitHub Desktop.
search term highlighting elasticpress
add_filter(
'ep_formatted_args',
function( $formatted_args ) {
if ( ! empty( $_GET['s'] ) ) {
foreach ( [ 'post_title', 'post_excerpt', 'author_name', 'terms.post_tag.name', 'terms.category.name' ] as $field ) {
$formatted_args['highlight']['fields'][ $field ] = [
'pre_tags' => [ '<strong style="background:yellow">' ],
'post_tags' => [ '</strong>' ],
'type' => 'plain',
];
}
}
return $formatted_args;
}
);
@tott
Copy link
Author

tott commented Sep 5, 2019

this will add highlight results in the elasticsearch response "highlight": { "post_title": [ "<strong style="background:yellow">Hello</strong> world!" ] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment