質問編集履歴

2

間違えたので削除しました。

2022/06/09 08:30

投稿

nao_tyrol
nao_tyrol

スコア16

test CHANGED
File without changes
test CHANGED
@@ -82,109 +82,3 @@
82
82
  よろしくお願いいたします。
83
83
 
84
84
 
85
- ====解決した方法を記載します====
86
- functions.phpに教えていただいたコードを指定するだけでは上手くいかず、時間がかかってしまいました。
87
- 私が上手くいった方法をメモしておきます。
88
-
89
- 結果的に、functions.phpに、関数を指定してphpに出力することで解決しました!
90
-
91
-
92
- ●ステップ1
93
- ベストアンサーに選ばせていただいた参考URLにこの方のコードを追記しました
94
- https://illbenet.jp/view/wordpress-get_terms_posttype
95
-
96
- すると、カスタム投稿タイプに属するタクソノミーのタームだけを表示することができます。
97
- ただ、この時点ではカウント数はタクソノミーが属する他のカスタム投稿の記事数もカウントされてしまいます。
98
- ここで2週間ほど試行錯誤して沼にハマりました。
99
-
100
- ●ステップ2
101
- こちらの方の記事を参考に、カウントする関数をfunctions.phpに作成したところ、思い通りの表示ができました!
102
- https://teratail.com/questions/51990?sort=3
103
-
104
- phpには下記のように記載しました。
105
- カスタム投稿タイプのslug と カスタムタクソノミーのslugは、その都度変更します。
106
- <?php echo getMyPostCount('カスタム投稿タイプのslug', 'カスタムタクソノミーのslug', $term->slug); ?>
107
-
108
-
109
- かなりハマって時間が掛かってしまったので、
110
- 同じように悩んでいる方の参考になれば幸いです。
111
-
112
-
113
- ▼functions.php に追記したコード
114
- ```ここに言語を入力
115
-
116
- // カスタム投稿タイプに属するタクソノミーのタームだけを表示
117
- function df_terms_clauses($clauses, $taxonomy, $args)
118
- {
119
- if (isset($args['post_type']) && !empty($args['post_type']) && $args['fields'] !== 'count') {
120
- global $wpdb;
121
-
122
- $post_types = [];
123
-
124
- if (is_array($args['post_type'])) {
125
- foreach ($args['post_type'] as $cpt) {
126
- $post_types[] = "'".$cpt."'";
127
- }
128
- } else {
129
- $post_types[] = "'".$args['post_type']."'";
130
- }
131
-
132
- if (!empty($post_types)) {
133
- $clauses['fields'] = 'DISTINCT '.str_replace('tt.*', 'tt.term_taxonomy_id, tt.taxonomy, tt.description, tt.parent', $clauses['fields']).', COUNT(p.post_type) AS count';
134
- $clauses['join'] .= ' LEFT JOIN '.$wpdb->term_relationships.' AS r ON r.term_taxonomy_id = tt.term_taxonomy_id LEFT JOIN '.$wpdb->posts.' AS p ON p.ID = r.object_id';
135
- $clauses['where'] .= 'AND p.post_status = "publish" AND (p.post_type IN ('.implode(',', $post_types).') OR p.post_type IS NULL)';
136
- $clauses['orderby'] = 'GROUP BY t.term_id '.$clauses['orderby'];
137
- }
138
- }
139
-
140
- return $clauses;
141
- }
142
-
143
- add_filter('terms_clauses', 'df_terms_clauses', 10, 3);
144
-
145
- // 指定のカスタム投稿タイプとタクソノミー・タームに属する記事数を取得する関数
146
- // この関数を使うと上手くいきました!
147
- function getMyPostCount($post_type, $taxonomy, $terms)
148
- {
149
- $args = [
150
- 'post_type' => $post_type,
151
- 'tax_query' => [
152
- [
153
- 'taxonomy' => $taxonomy,
154
- 'field' => 'slug',
155
- 'terms' => $terms,
156
- ],
157
- ],
158
- ];
159
- $query = new WP_Query($args);
160
-
161
- return $query->found_posts;
162
- }
163
-
164
- ```
165
-
166
- ▼php に追記したコード
167
- ```ここに言語を入力
168
-      <?php if ($terms): ?>
169
- <ul class="terms sp_display_f f_wrap">
170
- <?php
171
- $args = [
172
- 'post_type' => ['カスタム投稿タイプのslug'],
173
- 'hide_empty' => 0,
174
- 'orderby' => 'name',
175
- 'order' => 'ASC',
176
- ];
177
- $terms = get_terms('price', $args);
178
- ?>
179
- <?php foreach ($terms as $term): ?>
180
- <li>
181
- <input class="" type="checkbox" name="price[]" value="<?php echo esc_attr($term->slug); ?>">
182
- <?php echo esc_html($term->name); ?>
183
- (<?php echo getMyPostCount('カスタム投稿タイプのslug', 'カスタムタクソノミーのslug', $term->slug); ?>)
184
- </li>
185
- <?php endforeach; ?>
186
- </ul>
187
- <?php endif; ?>
188
- ```
189
-
190
-

1

解決した方法

2022/06/09 08:27

投稿

nao_tyrol
nao_tyrol

スコア16

test CHANGED
File without changes
test CHANGED
@@ -80,3 +80,111 @@
80
80
 
81
81
  ```
82
82
  よろしくお願いいたします。
83
+
84
+
85
+ ====解決した方法を記載します====
86
+ functions.phpに教えていただいたコードを指定するだけでは上手くいかず、時間がかかってしまいました。
87
+ 私が上手くいった方法をメモしておきます。
88
+
89
+ 結果的に、functions.phpに、関数を指定してphpに出力することで解決しました!
90
+
91
+
92
+ ●ステップ1
93
+ ベストアンサーに選ばせていただいた参考URLにこの方のコードを追記しました
94
+ https://illbenet.jp/view/wordpress-get_terms_posttype
95
+
96
+ すると、カスタム投稿タイプに属するタクソノミーのタームだけを表示することができます。
97
+ ただ、この時点ではカウント数はタクソノミーが属する他のカスタム投稿の記事数もカウントされてしまいます。
98
+ ここで2週間ほど試行錯誤して沼にハマりました。
99
+
100
+ ●ステップ2
101
+ こちらの方の記事を参考に、カウントする関数をfunctions.phpに作成したところ、思い通りの表示ができました!
102
+ https://teratail.com/questions/51990?sort=3
103
+
104
+ phpには下記のように記載しました。
105
+ カスタム投稿タイプのslug と カスタムタクソノミーのslugは、その都度変更します。
106
+ <?php echo getMyPostCount('カスタム投稿タイプのslug', 'カスタムタクソノミーのslug', $term->slug); ?>
107
+
108
+
109
+ かなりハマって時間が掛かってしまったので、
110
+ 同じように悩んでいる方の参考になれば幸いです。
111
+
112
+
113
+ ▼functions.php に追記したコード
114
+ ```ここに言語を入力
115
+
116
+ // カスタム投稿タイプに属するタクソノミーのタームだけを表示
117
+ function df_terms_clauses($clauses, $taxonomy, $args)
118
+ {
119
+ if (isset($args['post_type']) && !empty($args['post_type']) && $args['fields'] !== 'count') {
120
+ global $wpdb;
121
+
122
+ $post_types = [];
123
+
124
+ if (is_array($args['post_type'])) {
125
+ foreach ($args['post_type'] as $cpt) {
126
+ $post_types[] = "'".$cpt."'";
127
+ }
128
+ } else {
129
+ $post_types[] = "'".$args['post_type']."'";
130
+ }
131
+
132
+ if (!empty($post_types)) {
133
+ $clauses['fields'] = 'DISTINCT '.str_replace('tt.*', 'tt.term_taxonomy_id, tt.taxonomy, tt.description, tt.parent', $clauses['fields']).', COUNT(p.post_type) AS count';
134
+ $clauses['join'] .= ' LEFT JOIN '.$wpdb->term_relationships.' AS r ON r.term_taxonomy_id = tt.term_taxonomy_id LEFT JOIN '.$wpdb->posts.' AS p ON p.ID = r.object_id';
135
+ $clauses['where'] .= 'AND p.post_status = "publish" AND (p.post_type IN ('.implode(',', $post_types).') OR p.post_type IS NULL)';
136
+ $clauses['orderby'] = 'GROUP BY t.term_id '.$clauses['orderby'];
137
+ }
138
+ }
139
+
140
+ return $clauses;
141
+ }
142
+
143
+ add_filter('terms_clauses', 'df_terms_clauses', 10, 3);
144
+
145
+ // 指定のカスタム投稿タイプとタクソノミー・タームに属する記事数を取得する関数
146
+ // この関数を使うと上手くいきました!
147
+ function getMyPostCount($post_type, $taxonomy, $terms)
148
+ {
149
+ $args = [
150
+ 'post_type' => $post_type,
151
+ 'tax_query' => [
152
+ [
153
+ 'taxonomy' => $taxonomy,
154
+ 'field' => 'slug',
155
+ 'terms' => $terms,
156
+ ],
157
+ ],
158
+ ];
159
+ $query = new WP_Query($args);
160
+
161
+ return $query->found_posts;
162
+ }
163
+
164
+ ```
165
+
166
+ ▼php に追記したコード
167
+ ```ここに言語を入力
168
+      <?php if ($terms): ?>
169
+ <ul class="terms sp_display_f f_wrap">
170
+ <?php
171
+ $args = [
172
+ 'post_type' => ['カスタム投稿タイプのslug'],
173
+ 'hide_empty' => 0,
174
+ 'orderby' => 'name',
175
+ 'order' => 'ASC',
176
+ ];
177
+ $terms = get_terms('price', $args);
178
+ ?>
179
+ <?php foreach ($terms as $term): ?>
180
+ <li>
181
+ <input class="" type="checkbox" name="price[]" value="<?php echo esc_attr($term->slug); ?>">
182
+ <?php echo esc_html($term->name); ?>
183
+ (<?php echo getMyPostCount('カスタム投稿タイプのslug', 'カスタムタクソノミーのslug', $term->slug); ?>)
184
+ </li>
185
+ <?php endforeach; ?>
186
+ </ul>
187
+ <?php endif; ?>
188
+ ```
189
+
190
+