質問編集履歴

4

頂いた回答をもとに修正

2017/03/02 15:53

投稿

raidomaru
raidomaru

スコア106

test CHANGED
File without changes
test CHANGED
@@ -273,3 +273,195 @@
273
273
  }
274
274
 
275
275
  ```
276
+
277
+
278
+
279
+ ###修正したfuncstions.php
280
+
281
+ ```php
282
+
283
+ function my_get_year_archives( $args = '' ) {
284
+
285
+ global $wpdb, $wp_locale;
286
+
287
+
288
+
289
+ $defaults = array(
290
+
291
+ 'type' => 'monthly',
292
+
293
+ 'date_field' => 'startdate',
294
+
295
+ 'format' => 'option',
296
+
297
+ 'echo' => true,
298
+
299
+ 'limit' => '',
300
+
301
+ 'before' => '',
302
+
303
+ 'after' => '',
304
+
305
+ 'show_post_count' => true,
306
+
307
+ );
308
+
309
+
310
+
311
+ $r = wp_parse_args( $args, $defaults );
312
+
313
+ extract( $r, EXTR_SKIP );
314
+
315
+
316
+
317
+ if ( '' != $limit ) {
318
+
319
+ $limit = absint( $limit );
320
+
321
+ $limit = ' LIMIT '.$limit;
322
+
323
+ }
324
+
325
+
326
+
327
+ $field = 'm.meta_value';
328
+
329
+ // 修正 年と月を取ってyearmonthとする
330
+
331
+ $select = "SELECT SUBSTRING($field,1,6) AS `yearmonth`, count(p.ID) AS posts";
332
+
333
+ $where = "WHERE p.post_type = 'post' AND p.post_status = 'publish'";
334
+
335
+ $where .= $wpdb->prepare( ' AND m.meta_key = %s', $date_field );
336
+
337
+ $join = " INNER JOIN $wpdb->postmeta AS m ON m.post_id = p.ID";
338
+
339
+
340
+
341
+ $where = apply_filters( 'getarchives_where', $where, $r );
342
+
343
+ $join = apply_filters( 'getarchives_join' , $join , $r );
344
+
345
+
346
+
347
+ $output = '';
348
+
349
+ // 修正 年と月を取ってyearmonthとする
350
+
351
+ $query = "$select FROM $wpdb->posts AS p $join $where GROUP BY SUBSTRING($field,1,6) ORDER BY $field DESC $limit";
352
+
353
+ $key = md5( $query );
354
+
355
+ $cache = wp_cache_get( 'my_get_year_archives' , 'general' );
356
+
357
+ if ( !isset( $cache[ $key ] ) ) {
358
+
359
+ $arcresults = $wpdb->get_results( $query );
360
+
361
+ $cache[ $key ] = $arcresults;
362
+
363
+ wp_cache_set( 'my_get_year_archives', $cache, 'general' );
364
+
365
+ } else {
366
+
367
+ $arcresults = $cache[ $key ];
368
+
369
+ }
370
+
371
+
372
+
373
+ if ( $arcresults ) {
374
+
375
+ $afterafter = $after;
376
+
377
+ foreach ( (array) $arcresults as $arcresult ) {
378
+
379
+ // 修正 先頭から4つが年、5番目から2つが月
380
+
381
+ $arcresult->year = intval(mb_substr($arcresult->yearmonth, 0, 4));
382
+
383
+ $arcresult->month = intval(mb_substr($arcresult->yearmonth, 4, 2));
384
+
385
+ $url = add_query_arg( array( 'meta_key' => $date_field ), get_month_link( $arcresult->year, $arcresult->month ) );
386
+
387
+ // 修正 ここがプルダウン部分のテキストになる
388
+
389
+ $text = sprintf( '%s', $arcresult->year ).'年'.sprintf( '%02d', $arcresult->month ).'月';
390
+
391
+ if ($show_post_count)
392
+
393
+ $after = ' ('.$arcresult->posts.')' . $afterafter;
394
+
395
+ $output .= get_archives_link( $url, $text, $format, $before, $after );
396
+
397
+ }
398
+
399
+ }
400
+
401
+
402
+
403
+ if ( $echo )
404
+
405
+ echo $output;
406
+
407
+ else
408
+
409
+ return $output;
410
+
411
+ }
412
+
413
+
414
+
415
+ add_action( 'init', 'my_init' );
416
+
417
+ function my_init() {
418
+
419
+ global $wp;
420
+
421
+ $wp->add_query_var( 'meta_key' );
422
+
423
+ }
424
+
425
+
426
+
427
+ add_action( 'pre_get_posts', 'my_pre_get_posts' );
428
+
429
+ function my_pre_get_posts( $query ) {
430
+
431
+
432
+
433
+ if ( $query->is_month ) {
434
+
435
+ $meta_query = array(
436
+
437
+ array(
438
+
439
+ 'key' => $query->get( 'meta_key' ),
440
+
441
+ // 修正 カスタムフィールドの形式に合わせる
442
+
443
+ 'value' => $query->get( 'year' ).sprintf("%02d",$query->get( 'monthnum' )),
444
+
445
+ 'compare' => 'LIKE'
446
+
447
+ ),
448
+
449
+ );
450
+
451
+ $query->set( 'meta_query' , $meta_query );
452
+
453
+ $query->set( 'year' , '' );
454
+
455
+ // 修正
456
+
457
+ $query->set( 'monthnum' , '' );
458
+
459
+ $query->set( 'startdate' , '' );
460
+
461
+ $query->set( 'meta_key' , '' );
462
+
463
+ }
464
+
465
+ }
466
+
467
+ ```

3

修正点の追記、指摘事項を受けての修正をしました。

2017/03/02 15:53

投稿

raidomaru
raidomaru

スコア106

test CHANGED
File without changes
test CHANGED
@@ -12,8 +12,6 @@
12
12
 
13
13
  開始日のmeta_keyはstartdateです。
14
14
 
15
-
16
-
17
15
  また、イベントの種類によりカテゴリーを分けております。
18
16
 
19
17
 
@@ -40,6 +38,22 @@
40
38
 
41
39
 
42
40
 
41
+ 自身で行った変更点
42
+
43
+ 【category-A.php】
44
+
45
+ ・my_get_year_archives引数のmydateがカスタムフィールドのキーと考えstartdateに変更
46
+
47
+ 【functions.php】
48
+
49
+ ・<li>タグではなく<option>タグによる出力にしたかったので$default内のformatをhtmlからoptionに変更
50
+
51
+ ・$default内date_fieldをdateからstartdateに変更
52
+
53
+ ・件数表示させるために$default内show_post_countをfalseからtrueに変更
54
+
55
+
56
+
43
57
  これを月別、カテゴリー別にしたいです。
44
58
 
45
59
  テンプレートとして下記があります。
@@ -66,7 +80,13 @@
66
80
 
67
81
  <select id="archive" onchange="location.href=value">
68
82
 
83
+ <?
84
+
85
+ //php my_get_year_archives(array('date_field'=>'mydate',));
86
+
69
- <?php my_get_year_archives(array('date_field'=>'startdate',));?>
87
+ php my_get_year_archives(array('date_field'=>'startdate',));
88
+
89
+ ?>
70
90
 
71
91
  </select>
72
92
 
@@ -88,8 +108,12 @@
88
108
 
89
109
  'type' => 'monthly',
90
110
 
111
+ //カスタムフィールドのキー dateからstartdateに変更
112
+
91
113
  'date_field' => 'startdate',
92
114
 
115
+ //セレクトボックス内のoptionにするためhtmlから変更
116
+
93
117
  'format' => 'option',
94
118
 
95
119
  'echo' => true,
@@ -100,6 +124,8 @@
100
124
 
101
125
  'after' => '',
102
126
 
127
+ //件数表示をするためfalseから変更
128
+
103
129
  'show_post_count' => true,
104
130
 
105
131
  );
@@ -236,7 +262,9 @@
236
262
 
237
263
  $query->set( 'year' , '' );
238
264
 
265
+ //ご指摘頂きdateからstartdateに変更
266
+
239
- $query->set( 'date' , '' );
267
+ $query->set( 'startdate' , '' );
240
268
 
241
269
  $query->set( 'meta_key' , '' );
242
270
 

2

修正

2017/02/28 11:10

投稿

raidomaru
raidomaru

スコア106

test CHANGED
File without changes
test CHANGED
@@ -88,7 +88,7 @@
88
88
 
89
89
  'type' => 'monthly',
90
90
 
91
- 'date_field' => 'startdate2',
91
+ 'date_field' => 'startdate',
92
92
 
93
93
  'format' => 'option',
94
94
 

1

記入漏れ部分追記

2017/02/28 09:44

投稿

raidomaru
raidomaru

スコア106

test CHANGED
File without changes
test CHANGED
@@ -196,4 +196,52 @@
196
196
 
197
197
  }
198
198
 
199
+
200
+
201
+ add_action( 'init', 'my_init' );
202
+
203
+ function my_init() {
204
+
205
+ global $wp;
206
+
207
+ $wp->add_query_var( 'meta_key' );
208
+
209
+ }
210
+
211
+
212
+
213
+ add_action( 'pre_get_posts', 'my_pre_get_posts' );
214
+
215
+ function my_pre_get_posts( $query ) {
216
+
217
+ if ( $query->is_year ) {
218
+
219
+ $meta_query = array(
220
+
221
+ array(
222
+
223
+ 'key' => $query->get( 'meta_key' ),
224
+
225
+ 'value' => $query->get( 'year' ),
226
+
227
+ 'compare' => 'LIKE'
228
+
229
+ ),
230
+
231
+ );
232
+
233
+
234
+
235
+ $query->set( 'meta_query' , $meta_query );
236
+
237
+ $query->set( 'year' , '' );
238
+
239
+ $query->set( 'date' , '' );
240
+
241
+ $query->set( 'meta_key' , '' );
242
+
243
+ }
244
+
245
+ }
246
+
199
247
  ```