投稿(post)や投稿タイプで関連記事の一覧を表示する際に、同じカテゴリの記事を表示させるという事は多いです。
2つ以上のタクソノミーを使用した投稿で、全てのタクソノミーに属している記事の一覧を表示したい時の為のコードです。
単純なのですが、この答えにたどり着くのに時間がかかったので忘れないうちに残しておこうとおもいます。
関連記事を表示する方法
WordPress覚書 その3 「特定のタクソノミーに属するタームの取得と記事一覧の出力とか」を参考にしました。
○○○○:取得したいタームのあるタクソノミー
△:表示したい件数
□□□□:投稿タイプ(カスタム投稿タイプ)post等
<?php
$term_var = get_the_terms( $post->ID, '○○○○' );
$postQuery = new WP_Query();
$param = array(
'posts_per_page' => '△',
'post_type' => '□□□□',
'paged' => $paged,
'post__not_in' => array(get_the_ID()),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => '○○○○',
'field' => 'slug',
'terms' => $term_var[0]
)
)
);
$postQuery->query($param);
?>
<?php
if( $postQuery->have_posts() ) :
?>
関連イベント<br>
<?php
while( $postQuery->have_posts() ) :
$postQuery->the_post();
?>
<?php the_title(); ?><br>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
関連記事を複数表示する方法
上記のコードを基に「 $term_var = get_the_terms( $post->ID, ‘○○○○’ );」でしているタームを追加する事で複数指定出来るようになります。
こちらのコードでは「$term_var2」としていますが、arrayで指定する名前と一致していれば名前は変えても問題ありません。
relationの設定
AND:すべてに該当する投稿を取得する
OR:いずれかに該当する投稿を取得する
○○○○:1つ目の取得したいタームのあるタクソノミー
●●●●:2つ目の取得したいタームのあるタクソノミー
△:表示したい件数
□□□□:投稿タイプ(カスタム投稿タイプ)post等
<?php
$term_var = get_the_terms( $post->ID, '○○○○' );
$term_var2 = get_the_terms( $post->ID, '●●●●' );
$postQuery = new WP_Query();
$param = array(
'posts_per_page' => '△',
'post_type' => '□□□□',
'paged' => $paged,
'post__not_in' => array(get_the_ID()),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => '○○○○',
'field' => 'slug',
'terms' => $term_var[0]
),
array(
'taxonomy' => '●●●●',
'field' => 'slug',
'terms' => $term_var2[0]
)
)
);
$postQuery->query($param);
?>
<?php
if( $postQuery->have_posts() ) :
?>
過去に実施されたイベントレポート<br>
<?php
while( $postQuery->have_posts() ) :
$postQuery->the_post();
?>
<?php the_title(); ?><br>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
関連記事を複数表示する方法(タームを指定)
relationの設定
AND:すべてに該当する投稿を取得する
OR:いずれかに該当する投稿を取得する
○○○○:1つ目の取得したいタームのあるタクソノミー
●●●●:2つ目の取得したいタームのあるタクソノミー
▼▼▼▼:2つ目の取得したいターム
△:表示したい件数
□□□□:投稿タイプ(カスタム投稿タイプ)post等
<?php
$taxonomy_slug = '○○○○';
$post_type_slug = '□□□□';
$post_terms = wp_get_object_terms($post->ID, $taxonomy_slug);
if( $post_terms && !is_wp_error($post_terms)) {
$terms_slug = array();
foreach( $post_terms as $value ){
$terms_slug[] = $value->slug;
}
}
$args = array(
'post_type' => $post_type_slug,
'posts_per_page' => △,
'orderby' => 'rand',
'post__not_in' => array($post->ID),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => $taxonomy_slug,
'field' => 'slug',
'terms' => $terms_slug
),
array(
'taxonomy' => '●●●●',
'field' => 'slug',
'terms' => '▼▼▼▼',
)
)
);
$the_query = new WP_Query($args); if($the_query->have_posts()):
?>
関連イベント<br>
<?php while ($the_query->have_posts()): $the_query->the_post(); ?>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
大阪のホームページ制作会社TREVOでは、ホームページ制作に関する情報を掲載しています。最短2日で仮サイトを公開するサービスやSEO対策に特化したホームページ制作、オリジナルホームページデザイン、ライティング、リスティング広告、WEBマーケティングなどのサービスをご紹介しています。