Skip to content

Instantly share code, notes, and snippets.

@Jason-Murray

Jason-Murray/functions.php

Last active Jun 28, 2019
Embed
What would you like to do?
Adds all "visible" custom fields to the custom search excerpt for Relevanssi, code is based on the indexing code used in Relevanssi to add these fields to the index.
// Relevanssi add content to custom excerpts.
add_filter('relevanssi_excerpt_content', 'custom_fields_to_excerpts', 10, 3);
function custom_fields_to_excerpts($content, $post, $query) {
$custom_fields = get_post_custom_keys($post->ID);
$remove_underscore_fields = true;
if (is_array($custom_fields)) {
$custom_fields = array_unique($custom_fields); // no reason to index duplicates
foreach ($custom_fields as $field) {
if ($remove_underscore_fields) {
if (substr($field, 0, 1) == '_') continue;
}
$values = get_post_meta($post->ID, $field, false);
if ("" == $values) continue;
foreach ($values as $value) {
if ( !is_array ( $value ) ) {
$content .= " " . $value;
}
}
}
}
return $content;
}
@wpchannel

This comment has been minimized.

Copy link

@wpchannel wpchannel commented Jun 26, 2017

Awesome piece of code!

@joshespi

This comment has been minimized.

Copy link

@joshespi joshespi commented Aug 24, 2017

Works perfect! Thank you for posting this

@jasasoft

This comment has been minimized.

Copy link

@jasasoft jasasoft commented Aug 16, 2018

Thank you!

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