最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Only show posts with a specific term of an associated taxonomy in a custom post type archive

programmeradmin9浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

Try 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);
发布评论

评论列表(0)

  1. 暂无评论