質問編集履歴

1

具体的なコードを記述しました。

2019/07/21 03:41

投稿

mksk
mksk

スコア247

test CHANGED
File without changes
test CHANGED
@@ -55,3 +55,189 @@
55
55
 
56
56
 
57
57
  よろしくお願いします。
58
+
59
+
60
+
61
+
62
+
63
+ 具体的なコードは以下です。
64
+
65
+
66
+
67
+ ## wp_queryのmeta_queryにカスタムフィールドを指定して条件に合致するものを抽出
68
+
69
+
70
+
71
+ ```
72
+
73
+ <?php
74
+
75
+ // 絞り込み用パラメータを作成
76
+
77
+ $arr = [];
78
+
79
+ if (isset($_GET['sex'])) { // 性別
80
+
81
+ $sArray = [
82
+
83
+ 'key' => 'sex',
84
+
85
+ 'value' => $_GET['sex'],
86
+
87
+ 'compare' => '='
88
+
89
+ ];
90
+
91
+ $arr[] = $sArray;
92
+
93
+ }
94
+
95
+ if (isset($_GET['season'])) { // 季節
96
+
97
+ $seasonArray = [
98
+
99
+ 'key' => 'season',
100
+
101
+ 'value' => $_GET['season'],
102
+
103
+ 'compare' => '='
104
+
105
+ ];
106
+
107
+ $arr[] = $seasonArray;
108
+
109
+ }
110
+
111
+ if (isset($_GET['item'])) { // アイテム
112
+
113
+ $genreArray = [
114
+
115
+ 'key' => 'item',
116
+
117
+ 'value' => $_GET['item'],
118
+
119
+ 'compare' => '='
120
+
121
+ ];
122
+
123
+ $arr[] = $genreArray;
124
+
125
+ }
126
+
127
+ if (isset($_GET['color'])) { // カラー
128
+
129
+ $colorArray = [
130
+
131
+ 'key' => 'color',
132
+
133
+ 'value' => $_GET['color'],
134
+
135
+ 'compare' => '='
136
+
137
+ ];
138
+
139
+ $arr[] = $colorArray;
140
+
141
+ }
142
+
143
+
144
+
145
+ // 投稿データを取得
146
+
147
+ $args = array(
148
+
149
+ 'post_type' => 'post',
150
+
151
+ 'posts_per_page' => 10, // 1ページ10件掲載する
152
+
153
+ 'meta_query' => $argument
154
+
155
+ );
156
+
157
+ $my_query = new WP_Query($args);
158
+
159
+ while($my_query->have_posts()): $my_query->the_post();
160
+
161
+ // (以下ループ)
162
+
163
+ ```
164
+
165
+
166
+
167
+ 「赤いシャツ」のようなAND検索ではなく、「赤色」OR「シャツ」が返ってきました。
168
+
169
+
170
+
171
+
172
+
173
+ ## wp_queryで記事全件抽出 → 絞り込み条件に合致する記事を表示用配列に追加 → 表示用配列を回して記事を表示
174
+
175
+
176
+
177
+ ```
178
+
179
+ // 投稿データを取得
180
+
181
+ $args = array(
182
+
183
+ 'paged' => $paged,
184
+
185
+ 'post_type' => 'post',
186
+
187
+ 'posts_per_page' => -1, // 全件取得
188
+
189
+ );
190
+
191
+ $my_query = new WP_Query($args);
192
+
193
+ $posts = $my_query->get_posts();
194
+
195
+ $array = []; // 表示用配列
196
+
197
+
198
+
199
+ foreach ($posts as $post) {
200
+
201
+ $group = SCF::get('group'); // アイテムごとの繰り返しグループ
202
+
203
+ $sex = SCF::get('sex');
204
+
205
+ $season = SCF::get('season');
206
+
207
+ $item = SCF::get('item');
208
+
209
+ $color = SCF::get('color');
210
+
211
+
212
+
213
+ foreach ( $group as $fields ) {
214
+
215
+ if ($fields['sex'] == $_GET['sex'] && $fields['season'] == $_GET['season'] && $fields['item'] == $_GET['item'] && $fields['color'] == $_GET['color']) {
216
+
217
+ $array[] = $fields;
218
+
219
+ break;
220
+
221
+ }
222
+
223
+ if ($fields['sex'] == $_GET['sex'] && $fields['season'] == $_GET['season'] && $fields['item'] == $_GET['item']) {
224
+
225
+ $array[] = $fields;
226
+
227
+ break;
228
+
229
+ }
230
+
231
+ // ・・・(絞り込みパターン分繰り返し)
232
+
233
+ }
234
+
235
+ ```
236
+
237
+
238
+
239
+ 絞り込みパターン分記述がしなければならないのでしょうか・・・?
240
+
241
+
242
+
243
+ よろしくお願いします。