WordPress - ACF Repeater field into index

Hi, I have question about ACF plugin and repeater field. In documentation :https://community.algolia.com/wordpress/advanced-custom-fields.html there is clearly an option to index fields from ACF, which I was able to do. However how do I treat repeater fields and even more importantly how do I then work with them when searching? I have repeater field with schedules for location and date + time ranges which I need to be able to search. Is Algolia able to do such searches and if yes did anyone managed to put together working example? Thanks

Hi,

The repeater field shouldn’t behave any differently than standards ACF fields.

In this example, you probably get a string for the bio, if bio was a repeater field, you’ll simply get an array.

You can also modify the array. See the following example:

function my_post_attributes( array $attributes, WP_Post $post ) {

    $repeater = get_field( 'repeater_field_name', $post->ID );

    foreach ($repeater as &$item) {
        unset($item['useless']);
        $item['concat'] = $item['thing1'] . $item['thing2']
    }

    $attributes['repeater_field_name'] = $repeater;

    // Always return the value we are filtering.
    return $attributes;
}

About dates and location, it depends how you want to search in them. Do you simply want to get the result and use it at display or do you want to search per location?

I recommend this page, which explain how geo search works.
https://www.algolia.com/doc/guides/searching/geo-search/

Hi Julien, thank you for your reply. This was great help. I managed to make the example work after small modification:

function my_event_attributes( array $attributes, WP_Post $post ) {

if ( 'events' !== $post->post_type ) {

    return $attributes;
}


$attributes['description'] = get_field( 'event_description', $post->ID );
$attributes['time'] = get_field( 'event_time', $post->ID );
$attributes['date'] = get_field( 'event_date', $post->ID );
$repeater = get_field( 'event_schedules', $post->ID );




foreach ($repeater as $item) {
    unset($item['schedule_info']);
    
    
    $schedules = array ('location'=>$item['location'],'location_new'=>$item['location_new'],'event_date'=>$item['event_date'],'start_time'=>$item['start_time'],'end_time'=>$item['end_time'], 'all_day'=>$item['all_day'], 'cancel'=>$item['cancel'], 'is_location_change'=>$item['is_location_change']);
    

    
}



$attributes['event_schedules'] = $schedules;


return $attributes;

}

This will then give save one row of the repeater. It won’t save all the fields unfortunately. If I try to push it into multidimensional array it won’t do it, it will only show either NULL or array, array, array…

How do I display the results then on instasearch page?

I tried following:

<# if ( data.images.thumbnail ) { #>
{{ data.post_title }}
<# } #>
		<div class="ais-hits--content">
			<h2 itemprop="name headline"><a href="{{ data.permalink }}" title="{{ data.post_title }}" itemprop="url">{{{ data._highlightResult.post_title.value }}}</a></h2>
			<div class="excerpt">
				<p>
        <# if ( data._snippetResult['content'] ) { #>
          <span class="suggestion-post-content">{{{ data._snippetResult['content'].value }}}</span>
        <# } #>
				</p>
			</div>
            <div class="schedules">
                ----------
                <# if ( data.event_schedules ) { #>
                    this show is on:
                    <span class="event-schedules">{{ data.event_schedules.location.value }}</span>
                <# } #>
            </div>
		</div>
		<div class="ais-clearfix"></div>
	</article>

but my problem is that even the simple conditional <# if ( data.event_schedules ) { #> does not seem to be working, yet it is quite clear that the event_schedules do have data. Can you please let me know what am I missing here, how can I display the repeater from Algolia to the hit?

Many thanks for your help.

In your last snippet, does {{{ data._snippetResult['content'].value }}} work?

According to your screenshot, if you don’t have an array but one single object, this {{ data.event_schedules.location.value }} should be working :thinking:

Hi Julien,

Yes I would think so. None of the custom ACF fields are actually working. I have checked this by going through the ones that are indexed via Algolia by default. Those display with no issues. However when I try to display any ACF fields that are added via the function to attributes I am unable to display any of those in the instasearch template. Clearly as per your post I am trying to display them correctly.
Any thoughts?

EDIT:
Yes {{{ data._snippetResult[‘content’].value }}} works fine.
looking at the dashboard again the data seems to be pulled from :
sdfair_searchable_posts rather then from sdfair_posts_events
sdfair_searchable_posts does not contain any of the ACF attributes that I managed to push through events. Should I get rid of the events post type check wrapper? Is this the issue. But then I don’t have those ACF on other posts so they will unnecessary pollute those… is there any way around this?

add_filter( ‘algolia_post_shared_attributes’, ‘my_event_attributes’, 10, 2 );
add_filter( ‘algolia_searchable_post_shared_attributes’, ‘my_event_attributes’, 10, 2 );

function my_event_attributes( array $attributes, WP_Post $post ) {

if ( 'events' !== $post->post_type ) {

    return $attributes;
}


$attributes['description'] = get_field( 'event_description', $post->ID );
$attributes['time'] = get_field( 'event_time', $post->ID );
$attributes['date'] = get_field( 'event_date', $post->ID );
$repeater = get_field( 'event_schedules', $post->ID );

$x = 0;


foreach ($repeater as $item) {
    unset($item['schedule_info']);
    
    
    $schedules = array ('location'=>$item['location'],'location_new'=>$item['location_new'],'event_date'=>$item['event_date'],'start_time'=>$item['start_time'],'end_time'=>$item['end_time'], 'all_day'=>$item['all_day'], 'cancel'=>$item['cancel'], 'is_location_change'=>$item['is_location_change']);
    $attributes['event_schedules'][$x] = $schedules;
    $x = $x + 1;
    
    
}

Thank you