【wordpress】カスタム投稿の特定のカテゴリを表示する方法

こんにちは、nishi_talk(@nishi_talk)です。
今回はカスタム投稿の特定のカテゴリを表示する方法をご紹介します。 カスタム投稿が2タイプあった場合、それぞれを紐付けて表示する時の設定をします。
例としてカスタム投稿blogがあった場合、別の投稿タイプに特定のカテゴリ投稿を表示します。
まずはblogのカスタム投稿を設定します。 functions.phpに以下の記述を追加してカスタム投稿を設定します。
// ブログ
add_action('init', 'blog_custom_post_type');
function blog_custom_post_type() {
  $labels = array(
    'name'                => 'ブログ',
    'singular_name'       => 'ブログ',
    'add_new_item'        => '新しいブログを追加',
    'add_new'             => '新規追加',
    'new_item'            => '新しいブログ',
    'view_item'           => 'ブログを表示',
    'not_found'           => 'ブログはありません',
    'not_found_in_trash'  => 'ゴミ箱にブログはありません。',
    'search_items'        => 'ブログを検索',
  );
  $args = array(
    'labels'              => $labels,
    'public'              => true,
    'publicly_queryable'  => true,
    'show_ui'             => true,
    'query_var'           => true,
    'rewrite' => array('slug' => 'blog', 'with_front' => false),
    'hierarchical'        => false,
    'menu_position'       => 4,
    'has_archive'         => true,
    'yarpp_support'       => true,
    'capability_type'     => 'post',
    'supports' => array(
      'title',
      'editor',
      'thumbnail',
      'excerpt',
      'custom-fields'
      )
  );
  register_post_type('blog', $args);
  flush_rewrite_rules( true );
 
  register_taxonomy(
    'blog-taxonomy',
    array('blog'),
    array(
      'hierarchical' => true,
      'update_count_callback' => '_update_post_term_count',
      'label' => 'カテゴリー',
      'singular_label' => 'カテゴリー',
      'public' => true,
      'show_ui' => true,
    )
  );
}
今回は例として、カスタム投稿blogの特定のタクソノミーblog-taxonomyの投稿のみ別のカスタム投稿に表示させます。
カスタム投稿を表示したいページのテンプレートに以下の記述をします。
<?php
// ブログのカスタム投稿を取得
$args = array(
  'post_type' => 'blog',
  'tax_query' => array(
    array(
      'taxonomy' => 'blog-taxonomy'
    )
  )
);
$blog_post = get_posts($args);
?>
get_postsを使ってblogのカスタム投稿blog-taxonomyを取得します。

参考にさせていただいた記事

WordPressでカスタム投稿の特定のタームだけ出力する – Qiita
テンプレートタグ/get posts – WordPress Codex 日本語版