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

質問編集履歴

1

kei344 さんのアドバイスにより解決しました!!

2020/06/21 09:37

投稿

haru-_-ki
haru-_-ki

スコア2

title CHANGED
File without changes
body CHANGED
@@ -60,4 +60,60 @@
60
60
 
61
61
  親でセットした条件を継承しつつクエリを追加できるのでしょうか。
62
62
 
63
- よろしくお願いします。
63
+ よろしくお願いします。
64
+
65
+
66
+ ### 解決コード
67
+ ```
68
+ <!-- 新着記事を表示 -->
69
+ <?php
70
+ $args = array(
71
+ 'post_type' => 'post',
72
+ 'post_status' => 'publish',
73
+ 'category_name' => 'カテゴリ',
74
+ 'posts_per_page' => 6,
75
+ 'fields' => 'ids'
76
+ );
77
+ $the_query = new WP_Query($args);
78
+ $post_ids = $the_query->posts; // 除外記事を取得
79
+ ?>
80
+ <?php if($the_query->have_posts()): ?>
81
+ <?php while ($the_query->have_posts()): $the_query->the_post(); ?>
82
+ <!-- 投稿を表示 -->
83
+ <?php endwhile; ?>
84
+ <?php else: ?>
85
+ <!-- 投稿が無い場合 -->
86
+ <?php endif; ?>
87
+ <?php wp_reset_postdata(); ?>
88
+
89
+ <!-- 過去6ヶ月の記事をランダム表示 -->
90
+ <?php
91
+ $today = date('Y/m/d');
92
+ $setday = date('Y/m/d', strtotime('-6 month'));
93
+ $args = array(
94
+ 'post_type' => 'post',
95
+ 'post_status' => 'publish',
96
+ 'category_name' => 'カテゴリ',
97
+ 'date_query' => array(
98
+ array(
99
+ 'compare' => 'BETWEEN',
100
+ 'inclusive' => true,
101
+ 'before' => $today,
102
+ 'after' => $setday
103
+ ),
104
+ ),
105
+ 'orderby' => 'rand',
106
+ 'posts_per_page' => 6,
107
+ 'post__not_in' => $post_ids //除外記事
108
+ );
109
+ $the_query = new WP_Query($args);
110
+ ?>
111
+ <?php if($the_query->have_posts()): ?>
112
+ <?php while ($the_query->have_posts()): $the_query->the_post(); ?>
113
+ <!-- 投稿を表示 -->
114
+ <?php endwhile; ?>
115
+ <?php else: ?>
116
+ <!-- 投稿が無い場合 -->
117
+ <?php endif; ?>
118
+ <?php wp_reset_postdata(); ?>
119
+ ```