We already know how to query and get the posts using different methods in WordPress.

Today, I will show you how to get posts from a specific category of a custom post type and taxonomy. This can only be used in rear cases like as showing related posts that belongs to category named ‘related’, although that is not the proper way, but there are cases that you need to.

In this tutorial, I will still use the WP_Query class to perform a custom query.

<?php
$category_id = 11; //the category id
$taxonomy = 'news_category'; //the taxonomy where the category belongs
$post_type = 'news'; //the custom post type
$post_per_page = -1; //-1 shows all posts
$term = get_term( $category_id, $taxonomy );
$args = array(
		'post_type' => $post_type,
		'posts_per_page' => $post_per_page,
		'tax_query' => array(
							array(
								'taxonomy' => $taxonomy,
								'field' => 'slug',
								'terms' => $term->slug
							)
						)
	);

$wp_query = new WP_Query($args); ?>

<?php if ($wp_query->have_posts()) : ?>
	<ul>
	<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
		<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
	<?php endwhile; wp_reset_postdata(); ?>
	</ul>
<?php endif; ?>

In the above example, we use the get_term() function to get the category object. We then feed the term slug into tax_query slug parameter.