In this article, we will take a look into getting posts from a custom post type by a taxonomy, multiple taxonomy, multiple terms with a term id, slug, and name.
But before moving further, let’s assume I have a custom post type called Events, which has slug events.
I also have a taxonomy called Event categories, which has slug events-cat and this taxonomy has multiple terms. As shown in the image below.
Further in the article, will use the same case for all the examples.
Custom Post Type Posts by Taxonomy
To get the posts from a custom post type by taxonomy, we will use tax_query as an argument with WP_Query.
tax_query accept array of tax query arguments. The common tax_query arguments are:
- taxonomy: taxonomy slug, which is events-cat in our case.
- terms: taxonomy terms, like different types of events categories in our case.
- field: taxonomy term field: term_id, name, slug.
You can find more taxonomy parameter reference.
In the code below, I am getting posts from my custom post type Events by a taxonomy term ‘Meet Ups’.
<?php $args = array( 'post_type' => 'events', 'posts_per_page' => 5, 'tax_query' => array( array( 'taxonomy' => 'events-cat', // Taxonomy slug 'terms' => 'meet-ups', // Taxonomy term slug 'field' => 'slug', // Taxonomy field ) ) ); $query = new WP_Query($args); if($query->have_posts()): while($query->have_posts()): $query->the_post(); echo '<ul>'; echo '<li><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>'; echo '</ul>'; endwhile; /* Restore original Post Data */ wp_reset_postdata(); endif; ?>
Custom Post Type Posts by Term ID
To get the posts from custom post type by term id, you just need to use ‘field’ => ‘term_id’ and term id with terms like ‘terms’ = > 4 in the array of ‘tax_query’ arguments.
'tax_query' => array( array( 'taxonomy' => 'events-cat', 'terms' => 4, // term id 'field' => 'term_id', ) )
Custom Post Type Posts by Term name
To get the posts from custom post type by term name, you just need to use ‘field’ => ‘name’ and term name with terms like ‘terms’ = > ‘meet ups’ in the array of ‘tax_query’ arguments.
'tax_query' => array( array( 'taxonomy' => 'events-cat', 'terms' => 'meet ups', // term name 'field' => 'name', ) )
Custom Post Type Posts by Multiple Terms
To get the posts from a custom post types with multiple terms, you can use multiple terms as an array and assign it to the terms parameter. as shown in the code below.
'tax_query' => array( array( 'taxonomy' => 'events-cat', 'terms' => array('educational', 'meet-ups'), 'field' => 'slug' ) )
Custom Post Type Posts by Multiple Taxonomy
Let’s say, I have another taxonomy called Event Locations, that holds different locations for events, and now I want to get the posts by two of my taxonomies Event categories and Event locations.
So in that case, to get the posts from multiple taxonomies, I will use multiple taxonomy arrays with tax_query arguments.
Additionally, I can also use ‘relation’=>’AND’ or ‘relation’=>’OR’ as tax_query argument to filter the data from multiple taxonomies by relation between them.
'tax_query'=> array( // Taxonomy 1: Event Category array( 'taxonomy' => 'events-cat', 'terms' => 'meet-ups', 'field' => 'slug', ), // Taxonomy 2: Event Locations array( 'taxonomy' => 'events-location', 'terms' => 'newyork', 'field' => 'slug', ) 'relation' => 'AND', )