回答編集履歴

6

修正

2020/01/04 02:46

投稿

madone99
madone99

スコア1855

test CHANGED
@@ -48,10 +48,6 @@
48
48
 
49
49
  $result = new WP_Query(
50
50
 
51
-
52
-
53
- $result = new WP_Query(
54
-
55
51
  [
56
52
 
57
53
  'post_type' => 'property',

5

修正

2020/01/04 02:46

投稿

madone99
madone99

スコア1855

test CHANGED
@@ -46,7 +46,23 @@
46
46
 
47
47
 
48
48
 
49
- $result = new WP_Query();
49
+ $result = new WP_Query(
50
+
51
+
52
+
53
+ $result = new WP_Query(
54
+
55
+ [
56
+
57
+ 'post_type' => 'property',
58
+
59
+ 'paged' => $paged,
60
+
61
+ 'post_status' => 'publish'
62
+
63
+ ]
64
+
65
+ );
50
66
 
51
67
  $result->posts = array_unique( array_merge( $q1->posts, $q2->posts ), SORT_REGULAR );
52
68
 

4

修正

2020/01/04 02:40

投稿

madone99
madone99

スコア1855

test CHANGED
@@ -12,42 +12,42 @@
12
12
 
13
13
  ---
14
14
 
15
+ ## 修正
15
16
 
16
-
17
- これによって`is_search()`がtrueになりますので
18
-
19
- pre_get_postsに検索条件を入れた方がコードの見通も良くなると思います。
17
+ sの検索とmeta_queryをarray_mergeしてuniqueしてみてはどうでしょう?
20
18
 
21
19
 
22
20
 
23
21
  ```php
24
22
 
25
- function myPreGetPosts( $query ) {
23
+ $q1 = new WP_Query( array(
26
24
 
27
- if ( is_admin() || ! $query->is_main_query() ){
25
+ 'post_type' => 'property',
28
26
 
29
- return;
27
+ 's' => $s
30
28
 
31
- }
32
-
33
- if ( $query->is_search() ) {
34
-
35
- //検索条件
36
-
37
- }
38
-
39
- }
40
-
41
- add_action('pre_get_posts','myPreGetPosts');
42
-
43
- ```
29
+ ));
44
30
 
45
31
 
46
32
 
33
+ $q2 = new WP_Query( array(
34
+
35
+ 'post_type' => 'property',
36
+
37
+ 'meta_query' => array(
38
+
39
+ 'relation' => 'AND',
40
+
41
+ $metaquerysp,
42
+
43
+ )
44
+
47
- ---
45
+ ));
48
46
 
49
47
 
50
48
 
51
- 「いや、どうしてもPOSTでやりたい」というのであれば、送信先で
49
+ $result = new WP_Query();
52
50
 
53
- $_POSTからqueryを組み立て、search.phpのようなviewを作れば良いです。
51
+ $result->posts = array_unique( array_merge( $q1->posts, $q2->posts ), SORT_REGULAR );
52
+
53
+ ```

3

修正

2020/01/03 05:08

投稿

madone99
madone99

スコア1855

test CHANGED
@@ -1,4 +1,4 @@
1
- WordPressの検索機能を利用したいのでしたら
1
+ search.phpテンプレートでWordPressの検索機能を利用したいのでしたら
2
2
 
3
3
  サイトルート(https://example.comなど)に対してGET通信でsパラメータを送信する必要があります。
4
4
 
@@ -50,4 +50,4 @@
50
50
 
51
51
  「いや、どうしてもPOSTでやりたい」というのであれば、送信先で
52
52
 
53
- POSTから自分でqueryを組み立てれば良いです。
53
+ $_POSTからqueryを組み立て、search.phpのようなviewを作れば良いです。

2

修正

2020/01/02 13:58

投稿

madone99
madone99

スコア1855

test CHANGED
@@ -4,13 +4,19 @@
4
4
 
5
5
 
6
6
 
7
+ なので受け取る時はこうですね。
8
+
7
9
  `$s = $_GET['s'];`
10
+
11
+
12
+
13
+ ---
8
14
 
9
15
 
10
16
 
11
17
  これによって`is_search()`がtrueになりますので
12
18
 
13
- pre_get_postsに検索条件を入れた方が見通しも良くなると思います。
19
+ pre_get_postsに検索条件を入れた方がコードの見通しも良くなると思います。
14
20
 
15
21
 
16
22
 
@@ -35,3 +41,13 @@
35
41
  add_action('pre_get_posts','myPreGetPosts');
36
42
 
37
43
  ```
44
+
45
+
46
+
47
+ ---
48
+
49
+
50
+
51
+ 「いや、どうしてもPOSTでやりたい」というのであれば、送信先で
52
+
53
+ POSTから自分でqueryを組み立てれば良いです。

1

修正

2020/01/02 13:21

投稿

madone99
madone99

スコア1855

test CHANGED
@@ -1,7 +1,37 @@
1
1
  WordPressの検索機能を利用したいのでしたら
2
2
 
3
- index.phpにGETでsパラメータを送信する必要があります。
3
+ サイトルート(https://example.comなど)対してGET通信でsパラメータを送信する必要があります。
4
4
 
5
5
 
6
6
 
7
- `$s = $_GET['s']`
7
+ `$s = $_GET['s'];`
8
+
9
+
10
+
11
+ これによって`is_search()`がtrueになりますので
12
+
13
+ pre_get_postsに検索条件を入れた方が見通しも良くなると思います。
14
+
15
+
16
+
17
+ ```php
18
+
19
+ function myPreGetPosts( $query ) {
20
+
21
+ if ( is_admin() || ! $query->is_main_query() ){
22
+
23
+ return;
24
+
25
+ }
26
+
27
+ if ( $query->is_search() ) {
28
+
29
+ //検索条件
30
+
31
+ }
32
+
33
+ }
34
+
35
+ add_action('pre_get_posts','myPreGetPosts');
36
+
37
+ ```