admin管理员组文章数量:1321235
I have a custom post type "movie" with a taxonomy "period". The "period" taxonomy has two terms: "current" and "past".
Is it possible to only show posts with the "current" term in the "movies" archive page (archive-movie.php
)? If so, how?
I've used the template_include
filter before to show templates conditionally, but I'm not sure if this hook is useful in this case.
Any ideas?
I have a custom post type "movie" with a taxonomy "period". The "period" taxonomy has two terms: "current" and "past".
Is it possible to only show posts with the "current" term in the "movies" archive page (archive-movie.php
)? If so, how?
I've used the template_include
filter before to show templates conditionally, but I'm not sure if this hook is useful in this case.
Any ideas?
Share Improve this question asked Sep 26, 2020 at 18:54 leemonleemon 2,0324 gold badges25 silver badges51 bronze badges1 Answer
Reset to default 1Try adding to functions.php
function filter_movies( $query ) {
if( !is_admin() && $query->is_main_query() && $query->get('post_type') == 'movie') {
$tax_query = array(
array(
'taxonomy' => 'period',
'field' => 'slug',
'terms' => 'current',
),
);
$query->set( 'tax_query', $tax_query );
}
}
add_action('pre_get_posts', 'filter_movies', 9999);
本文标签: Only show posts with a specific term of an associated taxonomy in a custom post type archive
版权声明:本文标题:Only show posts with a specific term of an associated taxonomy in a custom post type archive 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742098902a2420710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论