【WP】投稿一覧で特定のカテゴリのみ表示、特定のカテゴリを除外して表示する方法

特定の投稿カテゴリで一覧表示、または特定の投稿カテゴリを除いて一覧表示させるには、
サブループを使います。
特定のカテゴリで一覧表示
特定のカテゴリースラッグを持つ投稿を取得
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $args = array( 'post_type' => 'post', // 投稿タイプ 'category_name' => 'news', // カテゴリースラッグ 'posts_per_page' => 10 // 表示件数 ); ?> <?php $query = new WP_Query( $args ); ?> <?php if($query -> have_posts()): ?> <!-- the loop --> <?php while($query -> have_posts()): $query->the_post();?> ループ内容 <?php endwhile; ?> <!-- end of the loop --> <?php wp_reset_postdata(); ?> <?php else : ?> <?php endif; ?> |
複数のカテゴリースラッグで絞り込む
|
1 2 3 4 5 6 7 |
<?php $args = array( 'post_type' => 'post', // 投稿タイプ 'category_name' => 'news,event' // カテゴリースラッグ 'posts_per_page' => 10 // 表示件数 ); ?> |
特定のカテゴリーIDを持つ投稿を取得
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $args = array( 'post_type' => 'post', // 投稿タイプ 'cat' => 1, // カテゴリーID 'posts_per_page' => 10 // 表示件数 ); ?> <?php $query = new WP_Query( $args ); ?> <?php if($query -> have_posts()): ?> <!-- the loop --> <?php while($query -> have_posts()): $query->the_post();?> ループ内容 <?php endwhile; ?> <!-- end of the loop --> <?php wp_reset_postdata(); ?> <?php else : ?> <?php endif; ?> |
複数のカテゴリーIDで絞り込む
|
1 2 3 4 5 6 7 |
<?php $args = array( 'post_type' => 'post', // 投稿タイプ 'cat' => '1,3', // カテゴリーID 'posts_per_page' => 10 // 表示件数 ); ?> |
特定のカテゴリを除いて一覧表示
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $args = array( 'post_type' => 'post', // 投稿タイプ category_not_in => array(1, 3), // 指定したカテゴリーIDを除外 'posts_per_page' => 10 // 表示件数 ); ?> <?php $query = new WP_Query( $args ); ?> <?php if($query -> have_posts()): ?> <!-- the loop --> <?php while($query -> have_posts()): $query->the_post();?> ループ内容 <?php endwhile; ?> <!-- end of the loop --> <?php wp_reset_postdata(); ?> <?php else : ?> <?php endif; ?> |
![MARKLEAPS[マークリープス]](https://markleaps.com/blog/wp-content/themes/mkl/images/00_logo.png)

![[WordPress] プラグイン「WP ALL Import」で既存記事を一括更新(CSV上書き)する方法](https://markleaps.com/blog/wp-content/uploads/2025/10/wp-all-import_update-500x254.jpg)
![[WordPress] CSVで記事を一括インポート-プラグイン「WP ALL Import」の使い方](https://markleaps.com/blog/wp-content/uploads/2025/10/wp-all-import-500x254.jpg)
![[WordPress] 記事への画像挿入方法](https://markleaps.com/blog/wp-content/uploads/2025/10/pic_manual_mainimg-500x254.png)
![[WordPress] カテゴリーとタグの違いと設定方法](https://markleaps.com/blog/wp-content/uploads/2025/10/wp-category-tag-500x254.jpg)