Skip to content

Instantly share code, notes, and snippets.

@jason-murray
Last active May 28, 2023 16:01
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jason-murray/122c9e5e79826fa85fcadef3d364fd52 to your computer and use it in GitHub Desktop.
Save jason-murray/122c9e5e79826fa85fcadef3d364fd52 to your computer and use it in GitHub Desktop.
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
Copy link

Awesome piece of code!

@joshespi
Copy link

joshespi commented Aug 24, 2017

Works perfect! Thank you for posting this

@jasasoft
Copy link

Thank you!

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