前提・実現したいこと
レンタルサーバーでWordPress5.2.1を使用しています。
こちらでAlgoliaを使用したサイト内検索をしたいと考えています。
Advanced Custom Fields バージョン 5.8.1 を使用しています。
https://ja.wordpress.org/plugins/advanced-custom-fields/
発生している問題・エラーメッセージ
エラーメッセージは出ていません。
Algolia for WordPress プラグインを入れ、API他の必要項目を入力し、インデックスを完了しました。
Search results は Use Algolia in the backend にチェックを入れています。
しかし、検索してみるとカスタムフィールドが検索対象になっていないようです。
検索対象はタイトルと記事内容(文章)のみになっているようです。
検索(インデックス)対象にカスタムフィールドを含めたいのですが、方法が分かりません。
試したこと
1.Algolia プラグインを入れた。
バージョン 3.0.2
https://ja.wordpress.org/plugins/wp-algolia/
2.テーマ内の function.php に下記のページに掲載されているコードを入れた。
https://community.algolia.com/wordpress/advanced-custom-fields.html
PHP
1add_filter( 'algolia_post_shared_attributes', 'my_post_attributes', 10, 2 ); 2add_filter( 'algolia_searchable_post_shared_attributes', 'my_post_attributes', 10, 2 ); 3 4/** 5 * @param array $attributes 6 * @param WP_Post $post 7 * 8 * @return array 9 */ 10function my_post_attributes( array $attributes, WP_Post $post ) { 11 12 if ( 'post' !== $post->post_type ) { 13 // We only want to add an attribute for the 'speaker' post type. 14 // Here the post isn't a 'speaker', so we return the attributes unaltered. 15 return $attributes; 16 } 17 18 // Get the field value with the 'get_field' method and assign it to the attributes array. 19 // @see https://www.advancedcustomfields.com/resources/get_field/ 20 $attributes['help'] = get_field( 'help', $post->ID ); 21 22 // Always return the value we are filtering. 23 return $attributes; 24}
PHP
1add_filter( 'algolia_posts_post_index_settings', 'my_posts_index_settings' ); 2// We could also have used a more general hook 'algolia_posts_index_settings', 3// but in that case we would have needed to add a check on the post type 4// to ensure we only adjust settings for the 'speaker' post_type. 5 6/** 7 * @param array $settings 8 * 9 * @return array 10 */ 11function my_posts_index_settings( array $settings ) { 12 13 // Make Algolia search into the 'bio' field when searching for results. 14 // Using ordered instead of unordered would make words matching in the beginning of the attribute 15 // make the record score higher. 16 // @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestoindex 17 $settings['attributesToIndex'][] = 'unordered(help)'; 18 19 // Make Algolia return a pre-computed snippet of 50 chars as part of the result set. 20 // @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestohighlight 21 $settings['attributesToSnippet'][] = 'help:50'; 22 23 // Always return the value we are filtering. 24 return $settings; 25}
3.Algolia プラグインで「Re-index」を行った。
補足情報(FW/ツールのバージョンなど)
サーバーの仕様
Apache 2.2
PHP 5.6.40
MySQL 5.5.62
回答2件
あなたの回答
tips
プレビュー