When working with Custom Post Types in WordPress, using custom taxonomies is a great way to organize content — like categories or tags, but fully customizable.
In this tutorial, you’ll learn how to display posts from a specific taxonomy term using WP_Query
. We’ll explain each step so even beginner developers can follow along with confidence.
🧠 What is a Custom Taxonomy?
A taxonomy in WordPress is a way to group content. The default taxonomies are:
category
post_tag
But you can also create your own — like genre
, type
, location
, or any grouping that fits your content.
For example: if you have a custom post type called project
, you might create a taxonomy called technology
to group projects by tech used (e.g., PHP, JavaScript, Python).
🧱 Step 1: Register a Custom Taxonomy (If Needed)
If you don’t already have a custom taxonomy, add this to your theme’s functions.php
:
function create_technology_taxonomy() {
register_taxonomy(
'technology',
'project',
[
'label' => 'Technology',
'hierarchical' => true,
'public' => true,
'rewrite' => ['slug' => 'technology'],
'show_in_rest' => true,
]
);
}
add_action('init', 'create_technology_taxonomy');
✅ This creates a technology
taxonomy linked to a custom post type project
.
🔄 Step 2: Query Posts by Specific Taxonomy Term
Now let’s say you want to show only the projects that use the JavaScript term in the technology taxonomy.
Here’s how you do it using WP_Query
:
<?php
$args = [
'post_type' => 'project',
'tax_query' => [
[
'taxonomy' => 'technology',
'field' => 'slug', // or 'term_id'
'terms' => 'javascript',
],
],
];
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post(); ?>
<article class="project">
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>">Read more</a>
</article>
<?php endwhile;
wp_reset_postdata();
else :
echo '<p>No projects found in this category.</p>';
endif;
?>
✅ Code Breakdown
'taxonomy'
: The name of the taxonomy (e.g.technology
)'field'
: You can filter byslug
orterm_id
'terms'
: The specific term you want to match (e.g.'javascript'
)
This fetches all project
posts that are assigned the javascript
term in the technology
taxonomy.
✅ Final Result
You now have a WordPress loop that:
- Filters posts by custom taxonomy
- Displays only the content related to a specific term
- Uses
WP_Query
andtax_query
properly
This is perfect for category pages, filterable archives, or custom sections on your homepage.
🧩 Extra Tips
- You can filter by multiple terms using
'terms' => ['javascript', 'php']
- Use
'operator' => 'AND'
or'IN'
to control how terms are matched - Always use
wp_reset_postdata()
after a custom query to avoid conflicts