質問編集履歴

9

試みたソースの更新

2019/06/10 10:45

投稿

munokura
munokura

スコア109

test CHANGED
File without changes
test CHANGED
@@ -56,7 +56,59 @@
56
56
 
57
57
  ```PHP
58
58
 
59
- add_action('admin_print_styles', 'user_profile_hide_style');
59
+ add_filter( 'algolia_post_shared_attributes', 'my_post_attributes', 10, 2 );
60
+
61
+ add_filter( 'algolia_searchable_post_shared_attributes', 'my_post_attributes', 10, 2 );
62
+
63
+
64
+
65
+ /**
66
+
67
+ * @param array $attributes
68
+
69
+ * @param WP_Post $post
70
+
71
+ *
72
+
73
+ * @return array
74
+
75
+ */
76
+
77
+ function my_post_attributes( array $attributes, WP_Post $post ) {
78
+
79
+
80
+
81
+ if ( 'post' !== $post->post_type ) {
82
+
83
+ // We only want to add an attribute for the 'speaker' post type.
84
+
85
+ // Here the post isn't a 'speaker', so we return the attributes unaltered.
86
+
87
+ return $attributes;
88
+
89
+ }
90
+
91
+
92
+
93
+ // Get the field value with the 'get_field' method and assign it to the attributes array.
94
+
95
+ // @see https://www.advancedcustomfields.com/resources/get_field/
96
+
97
+ $attributes['help'] = get_field( 'help', $post->ID );
98
+
99
+
100
+
101
+ // Always return the value we are filtering.
102
+
103
+ return $attributes;
104
+
105
+ }
106
+
107
+ ```
108
+
109
+
110
+
111
+ ```PHP
60
112
 
61
113
  add_filter( 'algolia_posts_post_index_settings', 'my_posts_index_settings' );
62
114
 
@@ -64,7 +116,9 @@
64
116
 
65
117
  // but in that case we would have needed to add a check on the post type
66
118
 
67
- // to ensure we only adjust settings for the 'textarea' post_type.
119
+ // to ensure we only adjust settings for the 'speaker' post_type.
120
+
121
+
68
122
 
69
123
  /**
70
124
 
@@ -78,6 +132,8 @@
78
132
 
79
133
  function my_posts_index_settings( array $settings ) {
80
134
 
135
+
136
+
81
137
  // Make Algolia search into the 'bio' field when searching for results.
82
138
 
83
139
  // Using ordered instead of unordered would make words matching in the beginning of the attribute
@@ -88,59 +144,15 @@
88
144
 
89
145
  $settings['attributesToIndex'][] = 'unordered(help)';
90
146
 
147
+
148
+
91
149
  // Make Algolia return a pre-computed snippet of 50 chars as part of the result set.
92
150
 
93
151
  // @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestohighlight
94
152
 
95
153
  $settings['attributesToSnippet'][] = 'help:50';
96
154
 
97
- // Always return the value we are filtering.
98
155
 
99
- return $settings;
100
-
101
- }
102
-
103
- ```
104
-
105
-
106
-
107
- ```PHP
108
-
109
- add_filter( 'algolia_posts_video_index_settings', 'my_video_index_settings' );
110
-
111
- // We could also have used a more general hook 'algolia_posts_index_settings',
112
-
113
- // but in that case we would have needed to add a check on the post type
114
-
115
- // to ensure we only adjust settings for the 'video' post_type.
116
-
117
- /**
118
-
119
- * @param array $settings
120
-
121
- *
122
-
123
- * @return array
124
-
125
- */
126
-
127
- function my_video_index_settings( array $settings ) {
128
-
129
- // By default, the plugin uses 'is_sticky' and the 'post_date' in the custom ranking.
130
-
131
- // Here we first retrieve the custom ranking to be able to alter it with more control.
132
-
133
- $customRanking = $settings['customRanking'];
134
-
135
- // We add our custom ranking rule at the beginning of the rules so that
136
-
137
- // it is the first one considered in the algorithm.
138
-
139
- array_unshift( $customRanking, 'desc(views_count)' );
140
-
141
- // We override the initial custom ranking rules.
142
-
143
- $settings['customRanking'] = $customRanking;
144
156
 
145
157
  // Always return the value we are filtering.
146
158
 

8

試みたソースの更新

2019/06/10 10:45

投稿

munokura
munokura

スコア109

test CHANGED
File without changes
test CHANGED
@@ -58,7 +58,7 @@
58
58
 
59
59
  add_action('admin_print_styles', 'user_profile_hide_style');
60
60
 
61
- add_filter( 'algolia_posts_textarea_index_settings', 'my_posts_index_settings' );
61
+ add_filter( 'algolia_posts_post_index_settings', 'my_posts_index_settings' );
62
62
 
63
63
  // We could also have used a more general hook 'algolia_posts_index_settings',
64
64
 

7

試みたソースの更新

2019/06/10 10:20

投稿

munokura
munokura

スコア109

test CHANGED
File without changes
test CHANGED
@@ -56,69 +56,15 @@
56
56
 
57
57
  ```PHP
58
58
 
59
- add_filter( 'algolia_post_shared_attributes', 'my_post_attributes', 10, 2 );
59
+ add_action('admin_print_styles', 'user_profile_hide_style');
60
60
 
61
- add_filter( 'algolia_searchable_post_shared_attributes', 'my_post_attributes', 10, 2 );
62
-
63
-
64
-
65
- /**
66
-
67
- * @param array $attributes
68
-
69
- * @param WP_Post $post
70
-
71
- *
72
-
73
- * @return array
74
-
75
- */
76
-
77
- function my_post_attributes( array $attributes, WP_Post $post ) {
78
-
79
-
80
-
81
- if ( 'speaker' !== $post->post_type ) {
82
-
83
- // We only want to add an attribute for the 'speaker' post type.
84
-
85
- // Here the post isn't a 'speaker', so we return the attributes unaltered.
86
-
87
- return $attributes;
88
-
89
- }
90
-
91
-
92
-
93
- // Get the field value with the 'get_field' method and assign it to the attributes array.
94
-
95
- // @see https://www.advancedcustomfields.com/resources/get_field/
96
-
97
- $attributes['bio'] = get_field( 'bio', $post->ID );
98
-
99
-
100
-
101
- // Always return the value we are filtering.
102
-
103
- return $attributes;
104
-
105
- }
106
-
107
- ```
108
-
109
-
110
-
111
- ```PHP
112
-
113
- add_filter( 'algolia_posts_speaker_index_settings', 'my_posts_index_settings' );
61
+ add_filter( 'algolia_posts_textarea_index_settings', 'my_posts_index_settings' );
114
62
 
115
63
  // We could also have used a more general hook 'algolia_posts_index_settings',
116
64
 
117
65
  // but in that case we would have needed to add a check on the post type
118
66
 
119
- // to ensure we only adjust settings for the 'speaker' post_type.
67
+ // to ensure we only adjust settings for the 'textarea' post_type.
120
-
121
-
122
68
 
123
69
  /**
124
70
 
@@ -132,8 +78,6 @@
132
78
 
133
79
  function my_posts_index_settings( array $settings ) {
134
80
 
135
-
136
-
137
81
  // Make Algolia search into the 'bio' field when searching for results.
138
82
 
139
83
  // Using ordered instead of unordered would make words matching in the beginning of the attribute
@@ -144,15 +88,59 @@
144
88
 
145
89
  $settings['attributesToIndex'][] = 'unordered(help)';
146
90
 
147
-
148
-
149
91
  // Make Algolia return a pre-computed snippet of 50 chars as part of the result set.
150
92
 
151
93
  // @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestohighlight
152
94
 
153
95
  $settings['attributesToSnippet'][] = 'help:50';
154
96
 
97
+ // Always return the value we are filtering.
155
98
 
99
+ return $settings;
100
+
101
+ }
102
+
103
+ ```
104
+
105
+
106
+
107
+ ```PHP
108
+
109
+ add_filter( 'algolia_posts_video_index_settings', 'my_video_index_settings' );
110
+
111
+ // We could also have used a more general hook 'algolia_posts_index_settings',
112
+
113
+ // but in that case we would have needed to add a check on the post type
114
+
115
+ // to ensure we only adjust settings for the 'video' post_type.
116
+
117
+ /**
118
+
119
+ * @param array $settings
120
+
121
+ *
122
+
123
+ * @return array
124
+
125
+ */
126
+
127
+ function my_video_index_settings( array $settings ) {
128
+
129
+ // By default, the plugin uses 'is_sticky' and the 'post_date' in the custom ranking.
130
+
131
+ // Here we first retrieve the custom ranking to be able to alter it with more control.
132
+
133
+ $customRanking = $settings['customRanking'];
134
+
135
+ // We add our custom ranking rule at the beginning of the rules so that
136
+
137
+ // it is the first one considered in the algorithm.
138
+
139
+ array_unshift( $customRanking, 'desc(views_count)' );
140
+
141
+ // We override the initial custom ranking rules.
142
+
143
+ $settings['customRanking'] = $customRanking;
156
144
 
157
145
  // Always return the value we are filtering.
158
146
 

6

試みたソースを変更

2019/06/10 10:15

投稿

munokura
munokura

スコア109

test CHANGED
File without changes
test CHANGED
@@ -142,7 +142,7 @@
142
142
 
143
143
  // @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestoindex
144
144
 
145
- $settings['attributesToIndex'][] = 'unordered(bio)';
145
+ $settings['attributesToIndex'][] = 'unordered(help)';
146
146
 
147
147
 
148
148
 
@@ -150,7 +150,7 @@
150
150
 
151
151
  // @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestohighlight
152
152
 
153
- $settings['attributesToSnippet'][] = 'bio:50';
153
+ $settings['attributesToSnippet'][] = 'help:50';
154
154
 
155
155
 
156
156
 

5

タグの追加

2019/06/10 10:07

投稿

munokura
munokura

スコア109

test CHANGED
File without changes
test CHANGED
File without changes

4

実現したいことの追記

2019/06/07 04:11

投稿

munokura
munokura

スコア109

test CHANGED
File without changes
test CHANGED
@@ -5,6 +5,10 @@
5
5
  レンタルサーバーでWordPress5.2.1を使用しています。
6
6
 
7
7
  こちらでAlgoliaを使用したサイト内検索をしたいと考えています。
8
+
9
+ Advanced Custom Fields バージョン 5.8.1 を使用しています。
10
+
11
+ https://ja.wordpress.org/plugins/advanced-custom-fields/
8
12
 
9
13
 
10
14
 

3

問題点を追記

2019/06/07 01:39

投稿

munokura
munokura

スコア109

test CHANGED
File without changes
test CHANGED
@@ -12,6 +12,8 @@
12
12
 
13
13
 
14
14
 
15
+ エラーメッセージは出ていません。
16
+
15
17
  Algolia for WordPress プラグインを入れ、API他の必要項目を入力し、インデックスを完了しました。
16
18
 
17
19
  Search results は Use Algolia in the backend にチェックを入れています。
@@ -19,6 +21,8 @@
19
21
 
20
22
 
21
23
  しかし、検索してみるとカスタムフィールドが検索対象になっていないようです。
24
+
25
+ 検索対象はタイトルと記事内容(文章)のみになっているようです。
22
26
 
23
27
 
24
28
 

2

試みたことを修正

2019/06/07 01:36

投稿

munokura
munokura

スコア109

test CHANGED
File without changes
test CHANGED
@@ -44,9 +44,9 @@
44
44
 
45
45
 
46
46
 
47
- <code><?php
48
47
 
49
48
 
49
+ ```PHP
50
50
 
51
51
  add_filter( 'algolia_post_shared_attributes', 'my_post_attributes', 10, 2 );
52
52
 
@@ -94,13 +94,13 @@
94
94
 
95
95
  return $attributes;
96
96
 
97
+ }
98
+
97
- }</code>
99
+ ```
98
100
 
99
101
 
100
102
 
101
- <code><?php
103
+ ```PHP
102
-
103
-
104
104
 
105
105
  add_filter( 'algolia_posts_speaker_index_settings', 'my_posts_index_settings' );
106
106
 
@@ -150,7 +150,9 @@
150
150
 
151
151
  return $settings;
152
152
 
153
+ }
154
+
153
- }</code>
155
+ ```
154
156
 
155
157
 
156
158
 

1

試みたことを修正

2019/06/07 01:31

投稿

munokura
munokura

スコア109

test CHANGED
File without changes
test CHANGED
@@ -30,7 +30,131 @@
30
30
 
31
31
 
32
32
 
33
+ 1.Algolia プラグインを入れた。
34
+
35
+ バージョン 3.0.2
36
+
37
+ https://ja.wordpress.org/plugins/wp-algolia/
38
+
39
+
40
+
41
+ 2.テーマ内の function.php に下記のページに掲載されているコードを入れた。
42
+
43
+ https://community.algolia.com/wordpress/advanced-custom-fields.html
44
+
45
+
46
+
47
+ <code><?php
48
+
49
+
50
+
51
+ add_filter( 'algolia_post_shared_attributes', 'my_post_attributes', 10, 2 );
52
+
53
+ add_filter( 'algolia_searchable_post_shared_attributes', 'my_post_attributes', 10, 2 );
54
+
55
+
56
+
57
+ /**
58
+
59
+ * @param array $attributes
60
+
61
+ * @param WP_Post $post
62
+
63
+ *
64
+
65
+ * @return array
66
+
67
+ */
68
+
69
+ function my_post_attributes( array $attributes, WP_Post $post ) {
70
+
71
+
72
+
73
+ if ( 'speaker' !== $post->post_type ) {
74
+
75
+ // We only want to add an attribute for the 'speaker' post type.
76
+
77
+ // Here the post isn't a 'speaker', so we return the attributes unaltered.
78
+
79
+ return $attributes;
80
+
81
+ }
82
+
83
+
84
+
85
+ // Get the field value with the 'get_field' method and assign it to the attributes array.
86
+
87
+ // @see https://www.advancedcustomfields.com/resources/get_field/
88
+
89
+ $attributes['bio'] = get_field( 'bio', $post->ID );
90
+
91
+
92
+
93
+ // Always return the value we are filtering.
94
+
95
+ return $attributes;
96
+
97
+ }</code>
98
+
99
+
100
+
101
+ <code><?php
102
+
103
+
104
+
105
+ add_filter( 'algolia_posts_speaker_index_settings', 'my_posts_index_settings' );
106
+
107
+ // We could also have used a more general hook 'algolia_posts_index_settings',
108
+
109
+ // but in that case we would have needed to add a check on the post type
110
+
111
+ // to ensure we only adjust settings for the 'speaker' post_type.
112
+
113
+
114
+
115
+ /**
116
+
117
+ * @param array $settings
118
+
119
+ *
120
+
121
+ * @return array
122
+
123
+ */
124
+
125
+ function my_posts_index_settings( array $settings ) {
126
+
127
+
128
+
129
+ // Make Algolia search into the 'bio' field when searching for results.
130
+
131
+ // Using ordered instead of unordered would make words matching in the beginning of the attribute
132
+
133
+ // make the record score higher.
134
+
135
+ // @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestoindex
136
+
137
+ $settings['attributesToIndex'][] = 'unordered(bio)';
138
+
139
+
140
+
141
+ // Make Algolia return a pre-computed snippet of 50 chars as part of the result set.
142
+
143
+ // @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestohighlight
144
+
33
- WP Search with Algolia プラグインも入れてみましたが、こちらにはインデックスをさせる項目が見つかりませんでした。
145
+ $settings['attributesToSnippet'][] = 'bio:50';
146
+
147
+
148
+
149
+ // Always return the value we are filtering.
150
+
151
+ return $settings;
152
+
153
+ }</code>
154
+
155
+
156
+
157
+ 3.Algolia プラグインで「Re-index」を行った。
34
158
 
35
159
 
36
160