teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

無理な質問でした。

2019/12/16 06:22

投稿

tada_dell
tada_dell

スコア6

title CHANGED
File without changes
body CHANGED
@@ -13,114 +13,4 @@
13
13
  ---
14
14
 
15
15
  行き詰っております。
16
- 根本的に考えかたが間違ているのかもわかりせんが、どうか解決策をお願いできればと思います
16
+ 根本的に考えかたが間違ていました
17
-
18
- **■使用プラグイン**
19
- ・Advanced Custom Fields
20
- ・Custom Post Type Date Archives
21
- ・Custom Post Type Permalinks
22
- ・Custom Post Type UI
23
-
24
- 投稿タイプ:news_type
25
- カスタムフィールド名:schedule
26
-
27
- **■front-page.phpに記載している、news_typeの月別アーカイブ用**
28
- ```ここに言語を入力
29
- <?php my_get_year_archives('post_type=news_type'); ?>
30
- <?php my_get_year_archives(array(
31
- 'date_field' => 'schedule',));
32
- ?>
33
-
34
- ```
35
-
36
- **■以下function**
37
- ```ここに言語を入力
38
- function my_get_year_archives( $args = '' ) {
39
- global $wpdb, $wp_locale;
40
-
41
- $defaults = array(
42
- 'type' => 'monthly',
43
- 'date_field' => 'schedule',
44
- 'format' => 'html',
45
- 'echo' => true,
46
- 'limit' => '',
47
- 'before' => '',
48
- 'after' => '',
49
- 'show_post_count' => true,
50
- );
51
-
52
- $r = wp_parse_args( $args, $defaults );
53
- extract( $r, EXTR_SKIP );
54
-
55
- if ( '' != $limit ) {
56
- $limit = absint( $limit );
57
- $limit = ' LIMIT '.$limit;
58
- }
59
-
60
- $field = 'm.meta_value';
61
- $select = "SELECT SUBSTRING($field,1,6) AS `yearmonth`, count(p.ID) AS posts";
62
- $where = "WHERE p.post_type = 'post' AND p.post_status = 'publish'";
63
- $where .= $wpdb->prepare( ' AND m.meta_key = %s', $date_field );
64
- $join = " INNER JOIN $wpdb->postmeta AS m ON m.post_id = p.ID";
65
-
66
- $where = apply_filters( 'getarchives_where', $where, $r );
67
- $join = apply_filters( 'getarchives_join' , $join , $r );
68
-
69
- $output = '';
70
- $query = "$select FROM $wpdb->posts AS p $join $where GROUP BY SUBSTRING($field,1,6) ORDER BY $field DESC $limit";
71
- $key = md5( $query );
72
- $cache = wp_cache_get( 'my_get_year_archives' , 'general' );
73
- if ( !isset( $cache[ $key ] ) ) {
74
- $arcresults = $wpdb->get_results( $query );
75
- $cache[ $key ] = $arcresults;
76
- wp_cache_set( 'my_get_year_archives', $cache, 'general' );
77
- } else {
78
- $arcresults = $cache[ $key ];
79
- }
80
-
81
- if ( $arcresults ) {
82
- $afterafter = $after;
83
- foreach ( (array) $arcresults as $arcresult ) {
84
- $arcresult->year = intval(mb_substr($arcresult->yearmonth, 0, 4));
85
- $arcresult->month = intval(mb_substr($arcresult->yearmonth, 4, 2));
86
- $url = add_query_arg( array( 'meta_key' => $date_field ), get_month_link( $arcresult->year, $arcresult->month ) );
87
- $text = sprintf( '%s', $arcresult->year ).'年'.sprintf( '%02d', $arcresult->month ).'月';
88
- if ($show_post_count)
89
- $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
90
- $output .= get_archives_link( $url, $text, $format, $before, $after );
91
- }
92
- }
93
-
94
- if ( $echo )
95
- echo $output;
96
- else
97
- return $output;
98
- }
99
-
100
- add_action( 'init', 'my_init' );
101
- function my_init() {
102
- global $wp;
103
- $wp->add_query_var( 'meta_key' );
104
- }
105
-
106
-
107
- function my_pre_get_posts( $query ) {
108
-
109
- if ( $query->is_month ) {
110
- $meta_query = array(
111
- array(
112
- 'key' => $query->get( 'meta_key' ),
113
- 'value' => $query->get( 'year' ).sprintf("%02d",$query->get( 'monthnum' )),
114
- 'compare' => 'LIKE'
115
- ),
116
- );
117
- $query->set( 'meta_query' , $meta_query );
118
- $query->set( 'year' , '' );
119
- $query->set( 'monthnum' , '' );
120
- $query->set( 'schedule' , '' );
121
- $query->set( 'meta_key' , '' );
122
- $query->set('post_type', 'news_type');
123
- }
124
- }
125
- add_action( 'pre_get_posts', 'my_pre_get_posts' );
126
- ```