最近、Algolia Search Wordpressプラグインをクライアント用の新しいWPビルドに実装しました。サイトは基本的に調査データを収集し、管理者に堅牢な検索機能を提供するために使用されます。Algolia Searchに自分のカスタムフィールドを認識させるのに問題がある
私は、各調査依頼ごとに固有の投稿を含むカスタム投稿タイプを作成しました。各調査入力フィールドは、ポスト内のカスタムフィールドにプッシュされます。
次の例では、Algoliaのカスタムフィールドプッシュと実装を実装しようとしています。ここにはhttps://community.algolia.com/wordpress/advanced-custom-fields.html#push-custom-fields-to-algoliaがあります。私のカスタムポストタイプは「Submissions」と題されており、そのポストタイプ内に '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