LoginSignup
1
2

More than 3 years have passed since last update.

WordPressで複数のカスタム投稿タイプの記事一覧を表示し、なおかつ特定のスラッグで絞り込む

Last updated at Posted at 2020-05-17

備忘録として。表題のとおり
「WordPressで複数のカスタム投稿タイプの記事一覧を表示し、なおかつ特定のカテゴリ(ターム)で絞り込む」をやりたいときに。

例えばカスタム投稿、以下の2つがあったとする。
「shop」
「news」
んで、この中の「pickup」だけ集めてトップページに出したいときに使う。

<?php 
    $args=array(
          'post_type' => array('shop','news'), //カスタム投稿名を指定
          'tax_query' => array(
            'relation' => 'OR',
            array(
              'taxonomy' => 'shop-cat',//カスタム投稿内のカテゴリを指定
              'field' => 'slug',
              'terms' => 'pickup', //スラッグ名Aを指定
              ),     
            array(
              'taxonomy' => 'news-cat',//カスタム投稿内のカテゴリを指定
              'field' => 'slug',
              'terms' => 'pickup', //スラッグ名Bを指定
              ),     
          ),
          'paged' => $paged,
          'posts_per_page' => '5' // 5件を取得 
   );
?>
      <?php query_posts( $args ); ?>
      <?php if(have_posts()): ?>// ループ開始
      <?php while(have_posts()):the_post(); ?>

これでできた!

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2