wp tax_query in
The tax_query
parameter in WordPress is used to filter posts by taxonomy terms. Here's an explanation for each step in using tax_query
:
- Declare the
tax_query
parameter within the arguments of theWP_Query
class. - Inside
tax_query
, specify an array of arrays to define the taxonomy query. Each inner array represents a single taxonomy query. - Each inner array should include the following parameters:
taxonomy
: Specify the taxonomy to query.field
: Define the field to be used for comparison (e.g., "term_id", "name", "slug", "term_taxonomy_id", "description").terms
: Specify the term(s) to include.operator
: Define the relationship between multiple conditions (e.g., "IN", "NOT IN", "AND", "EXISTS").include_children
: (optional) Set to true to include child terms.
Example:
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'news'
),
array(
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => array( 47, 49 ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );