The parse_tax_query WordPress PHP action fires after taxonomy-related query variables have been parsed.
Usage
add_action('parse_tax_query', 'your_custom_function');
function your_custom_function($query) {
// your custom code here
}
Parameters
$query(WP_Query): The WP_Query instance.
More information
See WordPress Developer Resources: parse_tax_query
Examples
Modify tax query for a custom post type
add_action('parse_tax_query', 'modify_tax_query_for_custom_post_type');
function modify_tax_query_for_custom_post_type($query) {
if ($query->is_main_query() && $query->is_post_type_archive('your_custom_post_type')) {
$tax_query = array(
array(
'taxonomy' => 'your_taxonomy',
'field' => 'slug',
'terms' => 'your_term',
),
);
$query->set('tax_query', $tax_query);
}
}
Exclude a category from the main query
add_action('parse_tax_query', 'exclude_category_from_main_query');
function exclude_category_from_main_query($query) {
if ($query->is_home() && $query->is_main_query()) {
$tax_query = array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'your_excluded_category',
'operator' => 'NOT IN',
),
);
$query->set('tax_query', $tax_query);
}
}
Show only posts with a specific tag on the search results page
add_action('parse_tax_query', 'filter_search_results_by_tag');
function filter_search_results_by_tag($query) {
if ($query->is_search()) {
$tax_query = array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'your_specific_tag',
),
);
$query->set('tax_query', $tax_query);
}
}
Filter the archive page to show posts from a specific category
add_action('parse_tax_query', 'filter_archive_page_by_category');
function filter_archive_page_by_category($query) {
if ($query->is_archive() && $query->is_main_query()) {
$tax_query = array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'your_specific_category',
),
);
$query->set('tax_query', $tax_query);
}
}
Exclude posts with a specific custom taxonomy term from the main query
add_action('parse_tax_query', 'exclude_custom_taxonomy_term_from_main_query');
function exclude_custom_taxonomy_term_from_main_query($query) {
if ($query->is_main_query()) {
$tax_query = array(
array(
'taxonomy' => 'your_custom_taxonomy',
'field' => 'slug',
'terms' => 'your_excluded_term',
'operator' => 'NOT IN',
),
);
$query->set('tax_query', $tax_query);
}
}