Custom taxonomy query for a custom post type

I have a custom post type called news with a custom taxonomy called news category. Right now I have a template called taxonomy-news_category-press.php where press is one of the news_categories. The query I’m using is:

$query_args = array(
    'taxonomy'  => 'news_category',
    'term'      => 'press',
    'post_type' => 'news',
)  

I don’t like this though. It’s hard coded and if I change press to something else it will break. I want to replace press in 'term' => 'press' with a variable that will pull in the news_category from the URL so it will be responsive with any news_category. I understand that I’ll need to change the template file to taxonomy-news_category.php when I solve this variable issue.

1 Answer
1

I figured it out. Thanks to an answer posted here. The code is

$term_slug  = get_query_var( 'term' );
$query_args = array(
    'taxonomy'   => 'news_category',
    'term'      => $term_slug,
    'post_type' => 'news'
 );

Leave a Comment