質問編集履歴
2
ソースが切れていたところを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -112,222 +112,284 @@
|
|
112
112
|
|
113
113
|
```php
|
114
114
|
|
115
|
+
/**
|
116
|
+
|
117
|
+
* サイト内検索の範囲に、カテゴリー名、タグ名、を含める
|
118
|
+
|
119
|
+
*/
|
120
|
+
|
121
|
+
function custom_search($search, $wp_query) {
|
122
|
+
|
123
|
+
global $wpdb;
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
//サーチページ以外だったら終了
|
128
|
+
|
115
129
|
if (!$wp_query->is_search)
|
116
130
|
|
117
|
-
|
131
|
+
return $search;
|
118
132
|
|
119
133
|
|
120
134
|
|
121
135
|
if (!isset($wp_query->query_vars))
|
122
136
|
|
123
|
-
|
137
|
+
return $search;
|
124
|
-
|
125
|
-
|
138
|
+
|
139
|
+
|
126
140
|
|
127
141
|
// ユーザー名とか、タグ名・カテゴリ名も検索対象に
|
128
142
|
|
129
143
|
$search_words = explode(' ', isset($wp_query->query_vars['s']) ? $wp_query->query_vars['s'] : '');
|
130
144
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
O
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
}
|
168
|
-
|
169
|
-
}
|
145
|
+
if ( count($search_words) > 0 ) {
|
146
|
+
|
147
|
+
$search = '';
|
148
|
+
|
149
|
+
foreach ( $search_words as $word ) {
|
150
|
+
|
151
|
+
if ( !empty($word) ) {
|
152
|
+
|
153
|
+
$search_word = $wpdb->escape("%{$word}%");
|
154
|
+
|
155
|
+
$search .= " AND (
|
156
|
+
|
157
|
+
{$wpdb->posts}.post_title LIKE '{$search_word}'
|
158
|
+
|
159
|
+
OR {$wpdb->posts}.post_content LIKE '{$search_word}'
|
160
|
+
|
161
|
+
OR {$wpdb->posts}.post_author IN (
|
162
|
+
|
163
|
+
SELECT distinct ID
|
164
|
+
|
165
|
+
FROM {$wpdb->users}
|
166
|
+
|
167
|
+
WHERE display_name LIKE '{$search_word}'
|
168
|
+
|
169
|
+
)
|
170
|
+
|
171
|
+
OR {$wpdb->posts}.ID IN (
|
172
|
+
|
173
|
+
SELECT distinct r.object_id
|
174
|
+
|
175
|
+
FROM {$wpdb->term_relationships} AS r
|
176
|
+
|
177
|
+
INNER JOIN {$wpdb->term_taxonomy} AS tt ON r.term_taxonomy_id = tt.term_taxonomy_id
|
178
|
+
|
179
|
+
INNER JOIN {$wpdb->terms} AS t ON tt.term_id = t.term_id
|
180
|
+
|
181
|
+
WHERE t.name LIKE '{$search_word}'
|
182
|
+
|
183
|
+
OR t.slug LIKE '{$search_word}'
|
184
|
+
|
185
|
+
OR tt.description LIKE '{$search_word}'
|
186
|
+
|
187
|
+
)
|
188
|
+
|
189
|
+
) ";
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
return $search;
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
add_filter('posts_search','custom_search', 10, 2);
|
204
|
+
|
205
|
+
```
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
プラグインの追加
|
210
|
+
|
211
|
+
下記のプラグインを追加しタグとカテゴリーを表示
|
212
|
+
|
213
|
+
search everything
|
214
|
+
|
215
|
+
https://ja.wordpress.org/plugins/search-everything/
|
216
|
+
|
217
|
+
![イメージ説明](8bb549075b34401ad4f0e754b9b93abf.png)
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
タイトルのみの設定をおこなったあと、下記のソースを入れてみましたが、
|
222
|
+
|
223
|
+
検索結果は本文もでてきてしまいました。
|
224
|
+
|
225
|
+
```ここに言語を入力
|
226
|
+
|
227
|
+
function posts_search_title_only( $orig_search, $query ) {
|
228
|
+
|
229
|
+
if ( $query->is_search() && $query->is_main_query() && ! is_admin() ) {
|
230
|
+
|
231
|
+
global $wpdb;
|
232
|
+
|
233
|
+
$search = '';
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
$q = $query->query_vars;
|
238
|
+
|
239
|
+
$n = ! empty( $q['exact'] ) ? '' : '%';
|
240
|
+
|
241
|
+
$searchand = '';
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
foreach ( $q['search_terms'] as $term ) {
|
246
|
+
|
247
|
+
$include = '-' !== substr( $term, 0, 1 );
|
248
|
+
|
249
|
+
if ( $include ) {
|
250
|
+
|
251
|
+
$like_op = 'LIKE';
|
252
|
+
|
253
|
+
$andor_op = 'OR';
|
254
|
+
|
255
|
+
} else {
|
256
|
+
|
257
|
+
$like_op = 'NOT LIKE';
|
258
|
+
|
259
|
+
$andor_op = 'AND';
|
260
|
+
|
261
|
+
$term = substr( $term, 1 );
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
$like = $n . $wpdb->esc_like( $term ) . $n;
|
266
|
+
|
267
|
+
// 検索対象をタイトルのみにします。
|
268
|
+
|
269
|
+
$search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title $like_op %s))", $like );
|
270
|
+
|
271
|
+
$searchand = ' AND ';
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
if ( ! empty( $search ) ) {
|
276
|
+
|
277
|
+
$search = " AND ({$search}) ";
|
278
|
+
|
279
|
+
if ( ! is_user_logged_in() )
|
280
|
+
|
281
|
+
$search .= " AND ($wpdb->posts.post_password = '') ";
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
return $search;
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
else {
|
290
|
+
|
291
|
+
return $orig_search;
|
170
292
|
|
171
293
|
}
|
172
294
|
|
173
|
-
|
295
|
+
}
|
296
|
+
|
174
|
-
|
297
|
+
add_filter( 'posts_search', 'posts_search_title_only', 10, 2 );
|
298
|
+
|
299
|
+
/**
|
300
|
+
|
301
|
+
* サイト内検索の範囲に、カテゴリー名、タグ名、を含める
|
302
|
+
|
303
|
+
*/
|
304
|
+
|
305
|
+
function custom_search($search, $wp_query) {
|
306
|
+
|
307
|
+
global $wpdb;
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
//サーチページ以外だったら終了
|
312
|
+
|
313
|
+
if (!$wp_query->is_search)
|
314
|
+
|
175
|
-
|
315
|
+
return $search;
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
if (!isset($wp_query->query_vars))
|
320
|
+
|
321
|
+
return $search;
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
// ユーザー名とか、タグ名・カテゴリ名も検索対象に
|
326
|
+
|
327
|
+
$search_words = explode(' ', isset($wp_query->query_vars['s']) ? $wp_query->query_vars['s'] : '');
|
328
|
+
|
329
|
+
if ( count($search_words) > 0 ) {
|
330
|
+
|
331
|
+
$search = '';
|
332
|
+
|
333
|
+
foreach ( $search_words as $word ) {
|
334
|
+
|
335
|
+
if ( !empty($word) ) {
|
336
|
+
|
337
|
+
$search_word = $wpdb->escape("%{$word}%");
|
338
|
+
|
339
|
+
$search .= " AND (
|
340
|
+
|
341
|
+
{$wpdb->posts}.post_title LIKE '{$search_word}'
|
342
|
+
|
343
|
+
OR {$wpdb->posts}.post_content LIKE '{$search_word}'
|
344
|
+
|
345
|
+
OR {$wpdb->posts}.post_author IN (
|
346
|
+
|
347
|
+
SELECT distinct ID
|
348
|
+
|
349
|
+
FROM {$wpdb->users}
|
350
|
+
|
351
|
+
WHERE display_name LIKE '{$search_word}'
|
352
|
+
|
353
|
+
)
|
354
|
+
|
355
|
+
OR {$wpdb->posts}.ID IN (
|
356
|
+
|
357
|
+
SELECT distinct r.object_id
|
358
|
+
|
359
|
+
FROM {$wpdb->term_relationships} AS r
|
360
|
+
|
361
|
+
INNER JOIN {$wpdb->term_taxonomy} AS tt ON r.term_taxonomy_id = tt.term_taxonomy_id
|
362
|
+
|
363
|
+
INNER JOIN {$wpdb->terms} AS t ON tt.term_id = t.term_id
|
364
|
+
|
365
|
+
WHERE t.name LIKE '{$search_word}'
|
366
|
+
|
367
|
+
OR t.slug LIKE '{$search_word}'
|
368
|
+
|
369
|
+
OR tt.description LIKE '{$search_word}'
|
370
|
+
|
371
|
+
)
|
372
|
+
|
373
|
+
) ";
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
}
|
176
378
|
|
177
379
|
}
|
178
380
|
|
381
|
+
|
382
|
+
|
383
|
+
return $search;
|
384
|
+
|
385
|
+
}
|
386
|
+
|
179
|
-
add_filter('posts_search','custom_search', 10, 2);
|
387
|
+
add_filter('posts_search','custom_search', 10, 2);
|
180
388
|
|
181
389
|
```
|
182
390
|
|
183
391
|
|
184
392
|
|
185
|
-
プラグインの追加
|
186
|
-
|
187
|
-
下記のプラグインを追加しタグとカテゴリーを表示
|
188
|
-
|
189
|
-
search everything
|
190
|
-
|
191
|
-
https://ja.wordpress.org/plugins/search-everything/
|
192
|
-
|
193
|
-
![イメージ説明](8bb549075b34401ad4f0e754b9b93abf.png)
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
タイトルのみの設定をおこなったあと、下記のソースを入れてみましたが、
|
198
|
-
|
199
|
-
検索結果は本文もでてきてしまいました。
|
200
|
-
|
201
|
-
```ここに言語を入力
|
202
|
-
|
203
|
-
function posts_search_title_only( $orig_search, $query ) {
|
204
|
-
|
205
|
-
if ( $query->is_search() && $query->is_main_query() && ! is_admin() ) {
|
206
|
-
|
207
|
-
global $wpdb;
|
208
|
-
|
209
|
-
$search = '';
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
$q = $query->query_vars;
|
214
|
-
|
215
|
-
$n = ! empty( $q['exact'] ) ? '' : '%';
|
216
|
-
|
217
|
-
$searchand = '';
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
foreach ( $q['search_terms'] as $term ) {
|
222
|
-
|
223
|
-
$include = '-' !== substr( $term, 0, 1 );
|
224
|
-
|
225
|
-
if ( $include ) {
|
226
|
-
|
227
|
-
$like_op = 'LIKE';
|
228
|
-
|
229
|
-
$andor_op = 'OR';
|
230
|
-
|
231
|
-
} else {
|
232
|
-
|
233
|
-
$like_op = 'NOT LIKE';
|
234
|
-
|
235
|
-
$andor_op = 'AND';
|
236
|
-
|
237
|
-
$term = substr( $term, 1 );
|
238
|
-
|
239
|
-
}
|
240
|
-
|
241
|
-
$like = $n . $wpdb->esc_like( $term ) . $n;
|
242
|
-
|
243
|
-
// 検索対象をタイトルのみにします。
|
244
|
-
|
245
|
-
$search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title $like_op %s))", $like );
|
246
|
-
|
247
|
-
$searchand = ' AND ';
|
248
|
-
|
249
|
-
}
|
250
|
-
|
251
|
-
if ( ! empty( $search ) ) {
|
252
|
-
|
253
|
-
$search = " AND ({$search}) ";
|
254
|
-
|
255
|
-
if ( ! is_user_logged_in() )
|
256
|
-
|
257
|
-
$search .= " AND ($wpdb->posts.post_password = '') ";
|
258
|
-
|
259
|
-
}
|
260
|
-
|
261
|
-
return $search;
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
else {
|
266
|
-
|
267
|
-
return $orig_search;
|
268
|
-
|
269
|
-
}
|
270
|
-
|
271
|
-
}
|
272
|
-
|
273
|
-
add_filter( 'posts_search', 'posts_search_title_only', 10, 2 );
|
274
|
-
|
275
|
-
$search_words = explode(' ', isset($wp_query->query_vars['s']) ? $wp_query->query_vars['s'] : '');
|
276
|
-
|
277
|
-
if ( count($search_words) > 0 ) {
|
278
|
-
|
279
|
-
$search = '';
|
280
|
-
|
281
|
-
foreach ( $search_words as $word ) {
|
282
|
-
|
283
|
-
if ( !empty($word) ) {
|
284
|
-
|
285
|
-
$search_word = $wpdb->escape("%{$word}%");
|
286
|
-
|
287
|
-
$search .= " AND (
|
288
|
-
|
289
|
-
{$wpdb->posts}.post_title LIKE '{$search_word}'
|
290
|
-
|
291
|
-
OR {$wpdb->posts}.post_content LIKE '{$search_word}'
|
292
|
-
|
293
|
-
OR {$wpdb->posts}.ID IN (
|
294
|
-
|
295
|
-
SELECT distinct r.object_id
|
296
|
-
|
297
|
-
FROM {$wpdb->term_relationships} AS r
|
298
|
-
|
299
|
-
INNER JOIN {$wpdb->term_taxonomy} AS tt ON r.term_taxonomy_id = tt.term_taxonomy_id
|
300
|
-
|
301
|
-
INNER JOIN {$wpdb->terms} AS t ON tt.term_id = t.term_id
|
302
|
-
|
303
|
-
WHERE t.name LIKE '{$search_word}'
|
304
|
-
|
305
|
-
OR t.slug LIKE '{$search_word}'
|
306
|
-
|
307
|
-
OR tt.description LIKE '{$search_word}'
|
308
|
-
|
309
|
-
)
|
310
|
-
|
311
|
-
) ";
|
312
|
-
|
313
|
-
}
|
314
|
-
|
315
|
-
}
|
316
|
-
|
317
|
-
}
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
return $search;
|
322
|
-
|
323
|
-
}
|
324
|
-
|
325
|
-
add_filter('posts_search','custom_search', 10, 2);
|
326
|
-
|
327
|
-
```
|
328
|
-
|
329
|
-
|
330
|
-
|
331
393
|
次にソースの部分をやめ
|
332
394
|
|
333
395
|
プラグインの追加でも試してみましたが、やはり本文のキーワードがでてきてしまいました。
|
1
ソースがとちゅうできれていたので
test
CHANGED
File without changes
|
test
CHANGED
@@ -110,6 +110,8 @@
|
|
110
110
|
|
111
111
|
以下2点を試しました。
|
112
112
|
|
113
|
+
```php
|
114
|
+
|
113
115
|
if (!$wp_query->is_search)
|
114
116
|
|
115
117
|
return $search;
|
@@ -124,8 +126,6 @@
|
|
124
126
|
|
125
127
|
// ユーザー名とか、タグ名・カテゴリ名も検索対象に
|
126
128
|
|
127
|
-
```php
|
128
|
-
|
129
129
|
$search_words = explode(' ', isset($wp_query->query_vars['s']) ? $wp_query->query_vars['s'] : '');
|
130
130
|
|
131
131
|
if ( count($search_words) > 0 ) {
|