WordPressのカテゴリー、タグアーカイブページにカスタム投稿タイプの投稿を含めるには

WordPressのカテゴリー、タグ一覧にカスタム投稿タイプの投稿を含める

WordPressでは通常、カテゴリー一覧やタグのアーカイブページではカスタム投稿タイプの記事が含まれません。

functions.php に以下の記述を追記しましょう。「カスタム投稿タイプ名」はご自分の投稿タイプスラッグに置き換えてください。

function add_customtype_archive( $wp_query ) {
    if ( is_admin() || ! $wp_query->is_main_query() )
        return;

    if ( $wp_query->is_category() || $wp_query->is_tag() ) {
        $wp_query->set( 'post_type', array( 'post', '「カスタム投稿タイプ名」' ));
        return;
    }
}
add_action( 'pre_get_posts', 'add_customtype_archive' );

投稿にカスタム投稿を含める場合

/* 【出力カスタマイズ】メインクエリーをカスタマイズ */
add_action( 'pre_get_posts', 'foo_modify_main_queries' );
function foo_modify_main_queries ( $query ) {
if ( ! is_admin() && $query->is_main_query() ) { // 管理画面以外かつメインクエリーを対象とする
if ( $query->is_home() ) {
$query->set( 'post_type', array('post','blog')); // 投稿とカスタム投稿(blog)を含める
}
}
}