Lets break this down, Advanced Custom Fields (ACF) does save the data as custom field/post meta, I guess it seems all to obvious, but it is better to confirm it, and ElasticPress does set up custom fields automatically to include them into the search index. ElasticPress does this unless the meta data is hidden, which is the case if the custom field name is prefixed by an underscore – _
.
As it is the case, that is just what ACF does, it prefixes its fields with Actually it does not or at least not for all. Not sure what the reason is, because I can’t take a look at the set-up. Anyway, the meta data gets correctly indexed, but the search doesn’t work, which will be resolved in another – link is following – question. All this is still apllying to adding additional, not automatically indexed, search data, like hidden custom fields._
making them hidden. Explained e.g. at the documentation page for update_field()
, shortly summarized you read there the field_key
is _
+ field_name
.
In reverse having the consequence that ACF fields are not being indexed by ElasticPress. As we can see if taking a look at the EP_API
class. The post meta does get included to the index like this:
'post_meta' => $this->prepare_meta( $post ),
So we take a closer look at the prepare_meta()
method, where we see:
if ( ! is_protected_meta( $key ) ) {
$prepared_meta[$key] = maybe_unserialize( $value );
}
Which confirms the suspected circumstance.
You have found the right place/hook to add the data nonetheless, being the ep_post_sync_args
filter. But you did not do it correctly, especially, you are trying to append a string to an array – see above code block and source. (Note: If you had debugging enabled you would have been bugged about doing something wrong.) This won’t work of course, you need to add to the $prepared_meta
array. I don’t know that much about ACF, so I leave that part out, but if you know how to get the data, then below exemplary code should make it pretty obvious to you, how to add the extra fields to the search index.
add_filter( 'ep_post_sync_args', 'wpse194785_ep_post_sync_args', 10, 2 );
function wpse194785_ep_post_sync_args( $post_args, $post_id ) {
$old_prepared_meta = $post_args[ 'post_meta' ];
$additional_prepared_meta = array();
// code to get up additional meta
// set up data like this:
// $additional_prepared_meta[ $key ] = array( $value );
// note that the value is enclosed into an array
// you can add one or multiple new elements by key => value association to the array
// afterwards merge new and old data
$new_prepared_meta = array_merge( $old_prepared_meta, $additional_prepared_meta );
$post_args[ 'post_meta' ] = $new_prepared_meta;
return $post_args;
}