回答編集履歴

1

ぞー修正例

2017/03/01 09:18

投稿

退会済みユーザー
test CHANGED
@@ -11,3 +11,199 @@
11
11
  $query->set( 'startdate' , '' );
12
12
 
13
13
  ```
14
+
15
+
16
+
17
+ **追記**
18
+
19
+ 月別にするなら、以下のような修正で。
20
+
21
+ ただし、カスタムフィールドの値は"2016年01月13日"のような形で、月はゼロ付きの二桁表記である必要があります。
22
+
23
+ もう少し改良を加えれば、上記の制限を解消して、カテゴリ別などもできるかと思います。
24
+
25
+ ```PHP
26
+
27
+ function my_get_year_archives( $args = '' ) {
28
+
29
+ global $wpdb, $wp_locale;
30
+
31
+
32
+
33
+ $defaults = array(
34
+
35
+ 'type' => 'monthly',
36
+
37
+ 'date_field' => 'startdate',
38
+
39
+ 'format' => 'option',
40
+
41
+ 'echo' => true,
42
+
43
+ 'limit' => '',
44
+
45
+ 'before' => '',
46
+
47
+ 'after' => '',
48
+
49
+ 'show_post_count' => true,
50
+
51
+ );
52
+
53
+
54
+
55
+ $r = wp_parse_args( $args, $defaults );
56
+
57
+ extract( $r, EXTR_SKIP );
58
+
59
+
60
+
61
+ if ( '' != $limit ) {
62
+
63
+ $limit = absint( $limit );
64
+
65
+ $limit = ' LIMIT '.$limit;
66
+
67
+ }
68
+
69
+
70
+
71
+ $field = 'm.meta_value';
72
+
73
+ // 修正
74
+
75
+ $select = "SELECT SUBSTRING($field,1,8) AS `yearmonth`, count(p.ID) AS posts";
76
+
77
+ $where = "WHERE p.post_type = 'post' AND p.post_status = 'publish'";
78
+
79
+ $where .= $wpdb->prepare( ' AND m.meta_key = %s', $date_field );
80
+
81
+ $join = " INNER JOIN $wpdb->postmeta AS m ON m.post_id = p.ID";
82
+
83
+
84
+
85
+ $where = apply_filters( 'getarchives_where', $where, $r );
86
+
87
+ $join = apply_filters( 'getarchives_join' , $join , $r );
88
+
89
+
90
+
91
+ $output = '';
92
+
93
+ // 修正
94
+
95
+ $query = "$select FROM $wpdb->posts AS p $join $where GROUP BY SUBSTRING($field,1,8) ORDER BY $field DESC $limit";
96
+
97
+ $key = md5( $query );
98
+
99
+ $cache = wp_cache_get( 'my_get_year_archives' , 'general' );
100
+
101
+ if ( !isset( $cache[ $key ] ) ) {
102
+
103
+ $arcresults = $wpdb->get_results( $query );
104
+
105
+ $cache[ $key ] = $arcresults;
106
+
107
+ wp_cache_set( 'my_get_year_archives', $cache, 'general' );
108
+
109
+ } else {
110
+
111
+ $arcresults = $cache[ $key ];
112
+
113
+ }
114
+
115
+
116
+
117
+ if ( $arcresults ) {
118
+
119
+ $afterafter = $after;
120
+
121
+ foreach ( (array) $arcresults as $arcresult ) {
122
+
123
+ // 修正
124
+
125
+ $arcresult->year = intval(mb_substr($arcresult->yearmonth, 0, 4));
126
+
127
+ $arcresult->month = intval(mb_substr($arcresult->yearmonth, 5, 2));
128
+
129
+ $url = add_query_arg( array( 'meta_key' => $date_field ), get_month_link( $arcresult->year, $arcresult->month ) );
130
+
131
+ $text = sprintf( '%s', $arcresult->yearmonth );
132
+
133
+ if ($show_post_count)
134
+
135
+ $after = ' ('.$arcresult->posts.')' . $afterafter;
136
+
137
+ $output .= get_archives_link( $url, $text, $format, $before, $after );
138
+
139
+ }
140
+
141
+ }
142
+
143
+
144
+
145
+ if ( $echo )
146
+
147
+ echo $output;
148
+
149
+ else
150
+
151
+ return $output;
152
+
153
+ }
154
+
155
+
156
+
157
+ add_action( 'init', 'my_init' );
158
+
159
+ function my_init() {
160
+
161
+ global $wp;
162
+
163
+ $wp->add_query_var( 'meta_key' );
164
+
165
+ }
166
+
167
+
168
+
169
+ add_action( 'pre_get_posts', 'my_pre_get_posts' );
170
+
171
+ function my_pre_get_posts( $query ) {
172
+
173
+
174
+
175
+ if ( $query->is_month ) {
176
+
177
+ $meta_query = array(
178
+
179
+ array(
180
+
181
+ 'key' => $query->get( 'meta_key' ),
182
+
183
+ // 修正
184
+
185
+ 'value' => $query->get( 'year' ).'年'.sprintf("%02d",$query->get( 'monthnum' )).'月',
186
+
187
+ 'compare' => 'LIKE'
188
+
189
+ ),
190
+
191
+ );
192
+
193
+ $query->set( 'meta_query' , $meta_query );
194
+
195
+ $query->set( 'year' , '' );
196
+
197
+ // 修正
198
+
199
+ $query->set( 'monthnum' , '' );
200
+
201
+ $query->set( 'startdate' , '' );
202
+
203
+ $query->set( 'meta_key' , '' );
204
+
205
+ }
206
+
207
+ }
208
+
209
+ ```