Taxonomy Parameters
Show posts associated with certain taxonomy.
{tax}
(string) – use taxonomy slug. (Deprecated since version 3.1 in favor of ‘tax_query
‘).tax_query
(array) – use taxonomy parameters (available since version 3.1).relation
(string) – The logical relationship between each inner taxonomy array when there is more than one. Possible values are ‘AND’, ‘OR’. Do not use with a single inner taxonomy array.taxonomy
(string) – Taxonomy.field
(string) – Select taxonomy term by. Possible values are ‘term_id’, ‘name’, ‘slug’ or ‘term_taxonomy_id’. Default value is ‘term_id’.terms
(int/string/array) – Taxonomy term(s).include_children
(boolean) – Whether or not to include children for hierarchical taxonomies. Defaults to true.operator
(string) – Operator to test. Possible values are ‘IN’, ‘NOT IN’, ‘AND’, ‘EXISTS’ and ‘NOT EXISTS’. Default value is ‘IN’.
Important Note: tax_query
takes an array of tax query arguments arrays (it takes an array of arrays).
This construct allows you to query multiple taxonomies by using the relation
parameter in the first (outer) array to describe the boolean relationship between the taxonomy arrays.
Simple Taxonomy Query:
Display posts tagged with bob, under people custom taxonomy:
1
2
3
4
5
6
7
8
9
10
11
|
$args = array ( 'post_type' => 'post' , 'tax_query' => array ( array ( 'taxonomy' => 'people' , 'field' => 'slug' , 'terms' => 'bob' , ), ), ); $query = new WP_Query( $args ); |
Multiple Taxonomy Handling:
Display posts from several custom taxonomies:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
$args = array ( 'post_type' => 'post' , 'tax_query' => array ( 'relation' => 'AND' , array ( 'taxonomy' => 'movie_genre' , 'field' => 'slug' , 'terms' => array ( 'action' , 'comedy' ), ), array ( 'taxonomy' => 'actor' , 'field' => 'term_id' , 'terms' => array ( 103, 115, 206 ), 'operator' => 'NOT IN' , ), ), ); $query = new WP_Query( $args ); |
Display posts that are in the quotes category OR have the quote post format:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
$args = array ( 'post_type' => 'post' , 'tax_query' => array ( 'relation' => 'OR' , array ( 'taxonomy' => 'category' , 'field' => 'slug' , 'terms' => array ( 'quotes' ), ), array ( 'taxonomy' => 'post_format' , 'field' => 'slug' , 'terms' => array ( 'post-format-quote' ), ), ), ); $query = new WP_Query( $args ); |