WordPress Algolia - 将 ACF 转发器字段索引为自定义属性
WordPress Algolia - indexing ACF repeater fields as custom attributes
我正在尝试将 ACF 字段作为自定义属性编制索引。根据 Algolia 文档 (https://community.algolia.com/wordpress/advanced-custom-fields.html),我设法索引了简单的 ACF 字段。我设法也索引了转发器字段,但我不确定这是否是最好的方法。有没有人设法解决了这个问题,并且不仅能够索引 ACF 转发器字段,而且后来在 WordPress 网站上搜索时将它们拉到 Instantsearch 模板上?
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;
}
return $attributes;
}
Algolia 仪表板
WordPress 后端和 Algolia 索引 CPT 的方式存在根本区别。您可以在插件设置 -> 自动完成中索引所有 CPT,您可以在插件设置 -> 搜索页面下索引一般主列表。如果您正在创建自己的搜索模板,则只能为您当前使用的特定 CPT 编制索引,但如果您正在使用默认搜索即时搜索模板,则需要为通用索引编制索引。
我正在尝试将 ACF 字段作为自定义属性编制索引。根据 Algolia 文档 (https://community.algolia.com/wordpress/advanced-custom-fields.html),我设法索引了简单的 ACF 字段。我设法也索引了转发器字段,但我不确定这是否是最好的方法。有没有人设法解决了这个问题,并且不仅能够索引 ACF 转发器字段,而且后来在 WordPress 网站上搜索时将它们拉到 Instantsearch 模板上?
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;
}
return $attributes;
}
Algolia 仪表板
WordPress 后端和 Algolia 索引 CPT 的方式存在根本区别。您可以在插件设置 -> 自动完成中索引所有 CPT,您可以在插件设置 -> 搜索页面下索引一般主列表。如果您正在创建自己的搜索模板,则只能为您当前使用的特定 CPT 编制索引,但如果您正在使用默认搜索即时搜索模板,则需要为通用索引编制索引。