最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - ElasticPress how to Include Meta to the mapping?

matteradmin8PV0评论

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.

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.

Share Improve this question asked Mar 21, 2019 at 22:16 Michael RogersMichael Rogers 5498 silver badges37 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

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.

Post a comment

comment list (0)

  1. No comments so far