Elasticpress: Adding content from Advanced Custom Fields to search index

Created on 21 Jul 2015  路  2Comments  路  Source: 10up/ElasticPress

I am trying to add content from some of my custom fields to the search index and failing. What am I missing?

function update_ep_sync_args( $post_args, $post_id ) {
    $oldPreparedMeta = $post_args[ 'post_meta' ];
    $additionalPreparedMeta = array();
    $post = get_post( $post_id );
    if( have_rows('content',$post->ID) ):
        // loop through the rows of data
        while ( have_rows('content',$post->ID) ) : the_row();
        // BODY TEXT
            if( get_row_layout() == 'text' ):
                $additionalPreparedMeta['text'] = get_sub_field('text') . ' ';
            elseif( get_row_layout() == 'subHeading' ):
                $text = get_sub_field('text');
                $additionalPreparedMeta['text'] = $text . ' ';
            endif;
        endwhile;
    endif;
    $newPreparedMeta = array_merge( $oldPreparedMeta, $additionalPreparedMeta );
    $post_args[ 'post_meta' ] = $newPreparedMeta;
    return $post_args;
}
add_filter( 'ep_post_sync_args', 'update_ep_sync_args', 10, 2 );

There's a modest bounty for whomever answers this question on StackExchange.

Most helpful comment

For those who arrive here (like I did) from a search because your Advanced Custom Fields are not working in ElasticPress, here's a tip that might get you on the right track:

For me, Mapping was working correctly. My issue was that Elastic Press does not search meta fields by default.

According to the documentation (https://github.com/10up/ElasticPress),
"The following are special parameters that are only supported by ElasticPress.
search_fields (array)
If not specified, defaults to array( 'post_title', 'post_excerpt', 'post_content' )."

See that section of the documentation, and/or the solution on this ticket for how to include meta fields in the search:

1067

All 2 comments

Never mind, the issue lies elsewhere.

For those who arrive here (like I did) from a search because your Advanced Custom Fields are not working in ElasticPress, here's a tip that might get you on the right track:

For me, Mapping was working correctly. My issue was that Elastic Press does not search meta fields by default.

According to the documentation (https://github.com/10up/ElasticPress),
"The following are special parameters that are only supported by ElasticPress.
search_fields (array)
If not specified, defaults to array( 'post_title', 'post_excerpt', 'post_content' )."

See that section of the documentation, and/or the solution on this ticket for how to include meta fields in the search:

1067

Was this page helpful?
0 / 5 - 0 ratings