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

回答編集履歴

5

コードの不備を修正

2015/07/20 14:49

投稿

flat
flat

スコア617

answer CHANGED
@@ -7,7 +7,7 @@
7
7
  */
8
8
  $today_news = new WP_Query(
9
9
  'post_type=news' .
10
- 'posts_per_page=-1' .
10
+ '&posts_per_page=-1' .
11
11
  '&year=' . date( 'Y' ) .
12
12
  '&monthnum=' . date( 'm' ) .
13
13
  '&day=' . date( 'd' )

4

変数名を修正

2015/07/20 14:49

投稿

flat
flat

スコア617

answer CHANGED
@@ -5,14 +5,14 @@
5
5
  * 見やすくするためにパラメータに改行とインデントを挿入しています。
6
6
  * 不要であれば1行にまとめて下さい。
7
7
  */
8
- $today_posts = new WP_Query(
8
+ $today_news = new WP_Query(
9
9
  'post_type=news' .
10
10
  'posts_per_page=-1' .
11
11
  '&year=' . date( 'Y' ) .
12
12
  '&monthnum=' . date( 'm' ) .
13
13
  '&day=' . date( 'd' )
14
14
  );
15
- $today_posts_count = $today_posts -> found_posts;
15
+ $today_news_count = $today_news -> found_posts;
16
16
  ```
17
17
 
18
18
  また、日付の指定には`date_query`を利用する事も出来ます。
@@ -28,8 +28,8 @@
28
28
  )
29
29
  )
30
30
  );
31
- $today_posts = new WP_Query( $args );
31
+ $today_news = new WP_Query( $args );
32
- $today_posts_count = $today_posts -> found_posts;
32
+ $today_news_count = $today_news -> found_posts;
33
33
  ```
34
34
 
35
35
  ちなみに、質問内容にある全記事数は[wp_count_posts](http://wpdocs.osdn.jp/テンプレートタグ/wp_count_posts)というテンプレートタグを利用する事でもっと簡単に取得出来ます。

3

参考URLを追加

2015/07/20 13:27

投稿

flat
flat

スコア617

answer CHANGED
@@ -32,7 +32,7 @@
32
32
  $today_posts_count = $today_posts -> found_posts;
33
33
  ```
34
34
 
35
- ちなみに、質問内容にある投稿の取得にもっンプルな方法があります。
35
+ ちなみに、質問内容にある全記事数は[wp_count_posts](http://wpdocs.osdn.jp/テンプレートタグ/wp_count_posts)いうテンプレートタグを利用する事でもっと簡単に取得出来ます。
36
36
  ```PHP
37
37
  // カスタム投稿タイプ news で公開済みになっている投稿の総数を文字列型で取得
38
38
  $news_count = wp_count_posts( 'news' ) -> publish;

2

コード追加

2015/07/20 10:11

投稿

flat
flat

スコア617

answer CHANGED
@@ -1,14 +1,15 @@
1
1
  `WP_Query`を利用する事で指定した日付の投稿の数を取得できます。
2
- 細かい指定を加えたい場合は[CodexにあるWP_Queryのページ](http://wpdocs.osdn.jp/関数リファレンス/WP_Query)を参照しながらパラメータを追加して下さい。
2
+ この他に細かい指定を加えたい場合は[CodexにあるWP_Queryのページ](http://wpdocs.osdn.jp/関数リファレンス/WP_Query)を参照しながらパラメータを追加して下さい。
3
3
  ```PHP
4
4
  /*
5
5
  * 見やすくするためにパラメータに改行とインデントを挿入しています。
6
6
  * 不要であれば1行にまとめて下さい。
7
7
  */
8
8
  $today_posts = new WP_Query(
9
+ 'post_type=news' .
9
- 'posts_per_page=-1' .
10
+ 'posts_per_page=-1' .
10
- '&year=' . date( 'Y' ) .
11
+ '&year=' . date( 'Y' ) .
11
- '&monthnum=' . date( 'm' ) .
12
+ '&monthnum=' . date( 'm' ) .
12
13
  '&day=' . date( 'd' )
13
14
  );
14
15
  $today_posts_count = $today_posts -> found_posts;
@@ -17,6 +18,7 @@
17
18
  また、日付の指定には`date_query`を利用する事も出来ます。
18
19
  ```PHP
19
20
  $args = array(
21
+ 'post_type' => 'news',
20
22
  'posts_per_page' => -1,
21
23
  'date_query' => array(
22
24
  array(

1

質問に関連した追記

2015/07/20 10:06

投稿

flat
flat

スコア617

answer CHANGED
@@ -1,4 +1,5 @@
1
1
  `WP_Query`を利用する事で指定した日付の投稿の数を取得できます。
2
+ 細かい指定を加えたい場合は[CodexにあるWP_Queryのページ](http://wpdocs.osdn.jp/関数リファレンス/WP_Query)を参照しながらパラメータを追加して下さい。
2
3
  ```PHP
3
4
  /*
4
5
  * 見やすくするためにパラメータに改行とインデントを挿入しています。
@@ -12,6 +13,7 @@
12
13
  );
13
14
  $today_posts_count = $today_posts -> found_posts;
14
15
  ```
16
+
15
17
  また、日付の指定には`date_query`を利用する事も出来ます。
16
18
  ```PHP
17
19
  $args = array(
@@ -26,4 +28,10 @@
26
28
  );
27
29
  $today_posts = new WP_Query( $args );
28
30
  $today_posts_count = $today_posts -> found_posts;
31
+ ```
32
+
33
+ ちなみに、質問内容にある投稿数の取得にはもっとシンプルな方法があります。
34
+ ```PHP
35
+ // カスタム投稿タイプ news で公開済みになっている投稿の総数を文字列型で取得
36
+ $news_count = wp_count_posts( 'news' ) -> publish;
29
37
  ```