質問編集履歴

1

無理な質問でした。

2019/12/16 06:22

投稿

tada_dell
tada_dell

スコア6

test CHANGED
File without changes
test CHANGED
@@ -28,224 +28,4 @@
28
28
 
29
29
  行き詰っております。
30
30
 
31
- 根本的に考えかたが間違ているのかもわかりせんが、どうか解決策をお願いできればと思います
31
+ 根本的に考えかたが間違ていました
32
-
33
-
34
-
35
- **■使用プラグイン**
36
-
37
- ・Advanced Custom Fields
38
-
39
- ・Custom Post Type Date Archives
40
-
41
- ・Custom Post Type Permalinks
42
-
43
- ・Custom Post Type UI
44
-
45
-
46
-
47
- 投稿タイプ:news_type
48
-
49
- カスタムフィールド名:schedule
50
-
51
-
52
-
53
- **■front-page.phpに記載している、news_typeの月別アーカイブ用**
54
-
55
- ```ここに言語を入力
56
-
57
- <?php my_get_year_archives('post_type=news_type'); ?>
58
-
59
- <?php my_get_year_archives(array(
60
-
61
- 'date_field' => 'schedule',));
62
-
63
- ?>
64
-
65
-
66
-
67
- ```
68
-
69
-
70
-
71
- **■以下function**
72
-
73
- ```ここに言語を入力
74
-
75
- function my_get_year_archives( $args = '' ) {
76
-
77
- global $wpdb, $wp_locale;
78
-
79
-
80
-
81
- $defaults = array(
82
-
83
- 'type' => 'monthly',
84
-
85
- 'date_field' => 'schedule',
86
-
87
- 'format' => 'html',
88
-
89
- 'echo' => true,
90
-
91
- 'limit' => '',
92
-
93
- 'before' => '',
94
-
95
- 'after' => '',
96
-
97
- 'show_post_count' => true,
98
-
99
- );
100
-
101
-
102
-
103
- $r = wp_parse_args( $args, $defaults );
104
-
105
- extract( $r, EXTR_SKIP );
106
-
107
-
108
-
109
- if ( '' != $limit ) {
110
-
111
- $limit = absint( $limit );
112
-
113
- $limit = ' LIMIT '.$limit;
114
-
115
- }
116
-
117
-
118
-
119
- $field = 'm.meta_value';
120
-
121
- $select = "SELECT SUBSTRING($field,1,6) AS `yearmonth`, count(p.ID) AS posts";
122
-
123
- $where = "WHERE p.post_type = 'post' AND p.post_status = 'publish'";
124
-
125
- $where .= $wpdb->prepare( ' AND m.meta_key = %s', $date_field );
126
-
127
- $join = " INNER JOIN $wpdb->postmeta AS m ON m.post_id = p.ID";
128
-
129
-
130
-
131
- $where = apply_filters( 'getarchives_where', $where, $r );
132
-
133
- $join = apply_filters( 'getarchives_join' , $join , $r );
134
-
135
-
136
-
137
- $output = '';
138
-
139
- $query = "$select FROM $wpdb->posts AS p $join $where GROUP BY SUBSTRING($field,1,6) ORDER BY $field DESC $limit";
140
-
141
- $key = md5( $query );
142
-
143
- $cache = wp_cache_get( 'my_get_year_archives' , 'general' );
144
-
145
- if ( !isset( $cache[ $key ] ) ) {
146
-
147
- $arcresults = $wpdb->get_results( $query );
148
-
149
- $cache[ $key ] = $arcresults;
150
-
151
- wp_cache_set( 'my_get_year_archives', $cache, 'general' );
152
-
153
- } else {
154
-
155
- $arcresults = $cache[ $key ];
156
-
157
- }
158
-
159
-
160
-
161
- if ( $arcresults ) {
162
-
163
- $afterafter = $after;
164
-
165
- foreach ( (array) $arcresults as $arcresult ) {
166
-
167
- $arcresult->year = intval(mb_substr($arcresult->yearmonth, 0, 4));
168
-
169
- $arcresult->month = intval(mb_substr($arcresult->yearmonth, 4, 2));
170
-
171
- $url = add_query_arg( array( 'meta_key' => $date_field ), get_month_link( $arcresult->year, $arcresult->month ) );
172
-
173
- $text = sprintf( '%s', $arcresult->year ).'年'.sprintf( '%02d', $arcresult->month ).'月';
174
-
175
- if ($show_post_count)
176
-
177
- $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
178
-
179
- $output .= get_archives_link( $url, $text, $format, $before, $after );
180
-
181
- }
182
-
183
- }
184
-
185
-
186
-
187
- if ( $echo )
188
-
189
- echo $output;
190
-
191
- else
192
-
193
- return $output;
194
-
195
- }
196
-
197
-
198
-
199
- add_action( 'init', 'my_init' );
200
-
201
- function my_init() {
202
-
203
- global $wp;
204
-
205
- $wp->add_query_var( 'meta_key' );
206
-
207
- }
208
-
209
-
210
-
211
-
212
-
213
- function my_pre_get_posts( $query ) {
214
-
215
-
216
-
217
- if ( $query->is_month ) {
218
-
219
- $meta_query = array(
220
-
221
- array(
222
-
223
- 'key' => $query->get( 'meta_key' ),
224
-
225
- 'value' => $query->get( 'year' ).sprintf("%02d",$query->get( 'monthnum' )),
226
-
227
- 'compare' => 'LIKE'
228
-
229
- ),
230
-
231
- );
232
-
233
- $query->set( 'meta_query' , $meta_query );
234
-
235
- $query->set( 'year' , '' );
236
-
237
- $query->set( 'monthnum' , '' );
238
-
239
- $query->set( 'schedule' , '' );
240
-
241
- $query->set( 'meta_key' , '' );
242
-
243
- $query->set('post_type', 'news_type');
244
-
245
- }
246
-
247
- }
248
-
249
- add_action( 'pre_get_posts', 'my_pre_get_posts' );
250
-
251
- ```