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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0