Last active
June 11, 2021 00:55
-
-
Save felipeelia/33dc3c92f96109066189d234878f7ca4 to your computer and use it in GitHub Desktop.
ElasticPress: Add specific post meta to search fields
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
<?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