質問編集履歴

2

「サブループ版」から「'query' => $the_query,」を削除

2019/01/13 11:50

投稿

makirons
makirons

スコア20

test CHANGED
File without changes
test CHANGED
@@ -22,8 +22,6 @@
22
22
 
23
23
  // 基本条件
24
24
 
25
- 'query' => $the_query,
26
-
27
25
  'paged' => $paged,
28
26
 
29
27
  'posts_per_page' => 10,

1

メインループ版2を追記しました。

2019/01/13 11:50

投稿

makirons
makirons

スコア20

test CHANGED
File without changes
test CHANGED
@@ -78,6 +78,8 @@
78
78
 
79
79
  function myPreGetPosts( $query ) {
80
80
 
81
+ $paged = get_query_var('paged') ? get_query_var('paged') : 1;
82
+
81
83
  // 管理画面
82
84
 
83
85
  if ( is_admin() || ! $query->is_main_query() ){
@@ -88,7 +90,7 @@
88
90
 
89
91
  // フロントページ
90
92
 
91
- if ( $query->is_home() ) {
93
+ if ( $query->is_home() ) {
92
94
 
93
95
  // 基本条件
94
96
 
@@ -141,3 +143,115 @@
141
143
 
142
144
 
143
145
  ```
146
+
147
+ ###メインループ版2
148
+
149
+ kei344様から頂戴したアドバイスを元にして、上の**メインループ版**を、次の**メインループ版2**に改良しました。(後述するエラーが出ます。)
150
+
151
+ ```php
152
+
153
+
154
+
155
+ function myPreGetPosts( $query ) {
156
+
157
+ $paged = get_query_var('paged') ? get_query_var('paged') : 1;
158
+
159
+ // 管理画面
160
+
161
+ if ( is_admin() || ! $query->is_main_query() ){
162
+
163
+ return;
164
+
165
+ }
166
+
167
+ // フロントページ
168
+
169
+ if ( $query->is_home() ) {
170
+
171
+ // 基本条件
172
+
173
+ $query->set('paged', $paged);
174
+
175
+ $query->set('posts_per_page', 10);
176
+
177
+ $query->set('post_type', 'mypost');
178
+
179
+ // 公開条件
180
+
181
+ $query -> set(
182
+
183
+ array( 'meta_query' => array(
184
+
185
+ array(
186
+
187
+ 'key' => 'targetIds',
188
+
189
+ 'value' => 1,
190
+
191
+ 'compare' => 'LIKE',
192
+
193
+ 'type'=>'NUMERIC'
194
+
195
+ ),
196
+
197
+ array(
198
+
199
+ 'key' => 'targetIds',
200
+
201
+ 'value' => '',
202
+
203
+ //'compare' => '=', // どちらにしても同じエラー
204
+
205
+ 'compare' => 'NOT EXISTS', // どちらにしても同じエラー
206
+
207
+ ),
208
+
209
+ 'relation'=>'OR'
210
+
211
+ ) )
212
+
213
+ );
214
+
215
+ }
216
+
217
+ }
218
+
219
+ add_action('pre_get_posts','myPreGetPosts');
220
+
221
+
222
+
223
+ ```
224
+
225
+ 上の**メインループ版2**によって次のエラーが出ます。
226
+
227
+ (``on line 231``というのは、``array( 'meta_query' => array(``の行です。)
228
+
229
+ ```エラー
230
+
231
+ Warning: Missing argument 2 for WP_Query::set(),
232
+
233
+ called in /export/user/c/zjp_95167c/live_77a5b6/var/wordpress/wp-content/themes/pingraphy-child/functions.php
234
+
235
+ on line 231
236
+
237
+ and defined in /export/user/c/zjp_95167c/live_77a5b6/var/wordpress/wp-includes/class-wp-query.php
238
+
239
+ on line 1604
240
+
241
+
242
+
243
+ Warning: Illegal offset type in
244
+
245
+ /export/user/c/zjp_95167c/live_77a5b6/var/wordpress/wp-includes/class-wp-query.php
246
+
247
+ on line 1605
248
+
249
+
250
+
251
+ Notice: Undefined variable: value in
252
+
253
+ /export/user/c/zjp_95167c/live_77a5b6/var/wordpress/wp-includes/class-wp-query.php
254
+
255
+ on line 1605
256
+
257
+ ```