Skip to content

Instantly share code, notes, and snippets.

@felipeelia
Last active June 11, 2021 00:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipeelia/33dc3c92f96109066189d234878f7ca4 to your computer and use it in GitHub Desktop.
Save felipeelia/33dc3c92f96109066189d234878f7ca4 to your computer and use it in GitHub Desktop.
ElasticPress: Add specific post meta to search fields
<?php
add_filter(
'ep_weighting_configuration_for_search',
function( $weighting_config ) {
global $wpdb;
$post_meta = get_transient( 'custom_ep_distinct_post_meta' );
if ( ! $post_meta ) {
$post_meta = $wpdb->get_col(
"SELECT DISTINCT meta_key
FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p ON pm.post_id = p.ID
WHERE meta_key like 'mdl_modules_%_text'"
);
set_transient( 'custom_ep_distinct_post_meta', $post_meta );
}
foreach ( $weighting_config as $post_type => $fields ) {
foreach ( $post_meta as $meta_key ) {
$weighting_config[ $post_type ][ "meta.{$meta_key}.value" ] = [
'enabled' => 1,
'weight' => 10,
];
}
}
return $weighting_config;
}
);
add_action(
'update_postmeta',
function( $meta_id, $object_id, $meta_key ) {
if ( preg_match( '/mdl_modules_([0-9]*)_text/', $meta_key ) ) {
delete_transient( 'custom_ep_distinct_post_meta' );
}
},
10,
3
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment