Last active
May 28, 2023 16:01
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
Works perfect! Thank you for posting this
Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome piece of code!