我有两种自定义帖子类型(系列和音频)和一种共享的自定义分类法。一个系列文章将有多个相关的音频文章。我正在尝试通过类别来关联它们。我想在系列页面上显示所有相关的音频帖子。我尝试遍历分类法,但是我获得了系列帖子的列表或所有系列和音频帖子。任何想法如何做到这一点?
答案 0 :(得分:0)
这行得通。
//get the post's Tax
$custom_terms = wp_get_post_terms($post->ID, 'series_taxonomy');
if( $custom_terms ){
// going to hold our tax_query params
$tax_query = array();
// add the relation parameter (not sure if it causes trouble if only 1 term so what the heck)
if( count( $custom_terms > 1 ) )
$tax_query['relation'] = 'OR' ;
// loop through venus and build a tax query
foreach( $custom_terms as $custom_term ) {
$tax_query[] = array(
'taxonomy' => 'series_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
);
}
// put all the WP_Query args together
$args = array( 'post_type' => 'sermon',
'posts_per_page' => 5,
'tax_query' => $tax_query );
// finally run the query
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
while( $loop->have_posts() ) : $loop->the_post(); ?>
<a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title(); ?></div></a>
<?php
endwhile;
}
wp_reset_query();
}?>