MENU

カスタム投稿タイプのタームに所属する記事件数を表示

<?php $query = new WP_Query(
  array(
  'post_type' => 'customposttype-slug', // カスタム投稿タイプのスラッグを指定
  'posts_per_page' => -1, // 全件表示
  'tax_query' => array(
    array(
    'taxonomy' => 'taxonomy_slug', // タクソノミースラッグを指定
    'field' => 'slug',
    'terms' => 'term_slug', // タームスラッグを指定
    ),
  ),
  ) );
?>

ターム:term_slug に登録されている記事数は <?php echo $query->found_posts; ?>件 です。

<ul>
  <?php if ( $query->have_posts() ): while ( $query->have_posts() ) : $query->the_post();?>
    <li>
      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
  <?php endwhile; endif; wp_reset_postdata(); ?>
</ul>
目次