Skip to content

Instantly share code, notes, and snippets.

@tott
Created September 21, 2020 12:19
Show Gist options
  • Save tott/1782a7fe1b8158eeb13ab632e2663574 to your computer and use it in GitHub Desktop.
Save tott/1782a7fe1b8158eeb13ab632e2663574 to your computer and use it in GitHub Desktop.
ElasticPress allow _wysiwig_ meta_keys in searches
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 '%_wysiwyg_%'"
);
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( '/_wysiwyg_(.*)/', $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