我最近將Algolia Search Wordpress插件實施到我的客戶端的新WP版本中。該網站將主要用於收集調查數據併爲其管理員提供強大的搜索功能。難以讓Algolia Search識別我的自定義字段
我已創建一個自定義帖子類型,其中包含每個調查提交的唯一帖子。每個調查輸入字段都會推送到帖子內的自定義字段。
在下面的示例中,我試圖實施Algolia的自定義字段推送和檢索功能:https://community.algolia.com/wordpress/advanced-custom-fields.html#push-custom-fields-to-algolia。我的自定義帖子類型標題爲'提交',我試圖在該帖子類型中使用'organisation_name'鍵來推送自定義字段。再次,我想我已經正確地設置了一切,但是這個自定義字段沒有被Algolia索引。任何幫助將不勝感激。
// Push custom fields to Algolia
add_filter('algolia_post_shared_attributes', 'my_post_attributes', 10, 2);
add_filter('algolia_searchable_post_shared_attributes', 'my_post_attributes', 10, 2);
/**
* @param array $attributes
* @param WP_Post $post
*
* @return array
*/
function my_post_attributes(array $attributes, WP_Post $post) {
if ('submissions' !== $post->post_type) {
// We only want to add an attribute for the 'submissions' post type.
// Here the post isn't a 'submission', so we return the attributes unaltered.
return $attributes;
}
// Get the field value with the 'get_field' method and assign it to the attributes array.
// @see https://www.advancedcustomfields.com/resources/get_field/
$attributes['organisation_name'] = get_field('organisation_name', $post->ID);
// Always return the value we are filtering.
return $attributes;
}
// Make custom fields searchable
add_filter('algolia_posts_index_settings', 'my_posts_index_settings');
// We could also have used a more general hook 'algolia_posts_index_settings',
// but in that case we would have needed to add a check on the post type
// to ensure we only adjust settings for the 'speaker' post_type.
/**
* @param array $settings
*
* @return array
*/
function my_posts_index_settings(array $settings) {
if ('submissions' !== $post->post_type) {
return $settings;
}
// Make Algolia search into the 'bio' field when searching for results.
// Using ordered instead of unordered would make words matching in the beginning of the attribute
// make the record score higher.
// @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestoindex
$settings['attributesToIndex'][] = 'unordered(organisation_name)';
// Make Algolia return a pre-computed snippet of 50 chars as part of the result set.
// @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestohighlight
$settings['attributesToSnippet'][] = 'organisation_name:50';
// Always return the value we are filtering.
return $settings;
}
你能確認你使用ACF插件嗎?另外,你是否在引入過濾器後重新編制索引? – rayrutjes