回答編集履歴

1

タグのついていない記事とその件数を表示するコードを追加

2019/02/04 19:38

投稿

unotalk
unotalk

スコア124

test CHANGED
@@ -1,13 +1,57 @@
1
+ **修正**
2
+
1
3
  > tag__not_inで除外した残りの投稿の件数を算出する方法があればそれを応用したい、となりますか。
2
4
 
3
5
 
4
6
 
5
- こちらについては$found_postsが使えるかと思います。
6
-
7
- get( $found_posts )で現在クエリパラメータにマッチす投稿数が取得できま
7
+ タグついていない記事を表示させ、その合計数を表示させるのであれば以下の方法で動きませんか
8
8
 
9
9
 
10
10
 
11
- 以下が参考になります。
11
+ ```php
12
12
 
13
+ <?php
14
+
15
+ $tags = get_terms('post_tag', array('fields'=>'ids') );
16
+
17
+ $args = array(
18
+
19
+ 'post_type' => 'post',
20
+
21
+ 'posts_per_page' => -1,
22
+
23
+ 'tax_query' => array(
24
+
25
+ array(
26
+
27
+ 'taxonomy' => 'post_tag',
28
+
29
+ 'field' => 'id',
30
+
31
+ 'terms' => $tags,
32
+
33
+ 'operator' => 'NOT IN'
34
+
35
+ )
36
+
37
+ )
38
+
39
+ );
40
+
41
+ $untagged = new WP_Query( $args );
42
+
43
+ while($untagged->have_posts()){
44
+
45
+ $untagged->the_post();
46
+
13
- [Class Reference/WP Query Methods_and_Properties](https://codex.wordpress.org/Class_Reference/WP_Query#Methods_and_Properties)
47
+ echo '<h3><a href="'.get_the_permalink().'">'.get_the_title().'</a></h3>';
48
+
49
+ echo "<br>";
50
+
51
+ }
52
+
53
+ echo 'タグのついていない投稿数:'.$untagged->found_posts;
54
+
55
+ ?>
56
+
57
+ ```