1

There's a plugin called ElasticPress it allows to connect an ElasticSearch server to WordPress. This plugin by default does not MAP Meta keys (custom fields) that begin with _ (underscore). But it has a filter to add any custom field to the mapping:

ep_prepare_meta_whitelist_key

My problem is that i don't understand how to use this filter to include my fields into the Mapping.

3

Solution 1

I haven't tested the plugin, but based on this code from the link in your question:

apply_filters( 'ep_prepare_meta_whitelist_key', false, $key, $post )

You can do something like this to explicitly allow a protected/private meta key:

add_filter( 'ep_prepare_meta_whitelist_key', function( $allow, $meta_key ){
    $meta_keys = ['_one', '_two', '_etc']; // meta keys you want to allow
    return in_array( $meta_key, $meta_keys );
}, 10, 2 );

Solution 2

Alternatively, looking at this part, you could use the ep_prepare_meta_allowed_protected_keys filter to add your custom protected meta keys to the array of index-able private meta keys:

add_filter( 'ep_prepare_meta_allowed_protected_keys', function( $meta_keys ){
    return array_merge( $meta_keys, ['_one', '_two', '_etc'] );
} );

Summary

If I were you, I'd use the second solution. But it's really up to you. :)

PS: The code would go in the theme functions file.

  • Thanks i really must learn to read filters. I'm trying to teach myself php but i can't figure this out yet. – Michael Rogers Mar 22 at 10:50
  • 1
    You're welcome. And this article may help you to better understand the hooks (actions and filters) in WordPress. – Sally CJ Mar 23 at 2:33
  • thanks i'll give it a read. – Michael Rogers Mar 23 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.