Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
// Add tag_slug__not_in to valid taxonomy queries
add_action('parse_tax_query', function($wp_query) {
$q = &$wp_query->query_vars;
$tax_query = array();
if (!empty($q['tag_slug__not_in'])) {
$q['tag_slug__not_in'] = array_map('sanitize_title_for_query', array_unique((array)$q['tag_slug__not_in']));
$tax_query[] = array(
'taxonomy' => 'post_tag',
'terms' => $q['tag_slug__not_in'],
'field' => 'slug',
'operator' => 'NOT IN'
);
}
// Merge with existing $wp_query->tax_query
if (!empty($tax_query)) {
$tmp_tax_query = new WP_Tax_Query($tax_query);
$wp_query->tax_query->queries = array_merge(
$wp_query->tax_query->queries,
$tmp_tax_query->queries
);
$wp_query->tax_query->queried_terms = array_merge(
$wp_query->tax_query->queried_terms,
$tmp_tax_query->queried_terms
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment