Override tax_query with pre_get_posts to include other term_ids on a single category
I'm trying to include more than one term_id(multiple checkboxes filter) on a single category page. I managed to recollect enough to build a tax_query with pre_get_posts, but now it seems, I have two tax_queries, one is in WP_Query-query_vars and the other is just in WP_Query(that one is of WP_Tax_Query type):
object(WP_Query)#1968 (50) {
[query]=
array(1) {
[product_cat]=
string(77) parent-category-slug/slug-of-the-category
}
[query_vars]=
array(59) {
[product_cat]=
string(56) slug-of-the-category
[error]=
string(0)
[m]=
string(0)
[p]=
int(0)
[post_parent]=
string(0)
[subpost]=
string(0)
[subpost_id]=
string(0)
[attachment]=
string(0)
[attachment_id]=
int(0)
[name]=
string(0)
[pagename]=
string(0)
[page_id]=
int(0)
[second]=
string(0)
[minute]=
string(0)
[hour]=
string(0)
[day]=
int(0)
[monthnum]=
int(0)
[year]=
int(0)
[w]=
int(0)
[category_name]=
string(0)
[tag]=
string(0)
[cat]=
string(0)
[tag_id]=
string(0)
[author]=
string(0)
[author_name]=
string(0)
[feed]=
string(0)
[tb]=
string(0)
[paged]=
int(0)
[meta_key]=
string(0)
[meta_value]=
string(0)
[preview]=
string(0)
[s]=
string(0)
[sentence]=
string(0)
[title]=
string(0)
[fields]=
string(0)
[menu_order]=
string(0)
[embed]=
string(0)
[category__in]=
array(0) {
}
[category__not_in]=
array(0) {
}
[category__and]=
array(0) {
}
[post__in]=
array(0) {
}
[post__not_in]=
array(0) {
}
[post_name__in]=
array(0) {
}
[tag__in]=
array(0) {
}
[tag__not_in]=
array(0) {
}
[tag__and]=
array(0) {
}
[tag_slug__in]=
array(0) {
}
[tag_slug__and]=
array(0) {
}
[post_parent__in]=
array(0) {
}
[post_parent__not_in]=
array(0) {
}
[author__in]=
array(0) {
}
[author__not_in]=
array(0) {
}
[orderby]=
string(16) menu_order title
[order]=
string(3) ASC
[meta_query]=
array(0) {
}
[tax_query]=
array(1) {
[0]=
array(4) {
[taxonomy]=
string(11) product_cat
[field]=
string(7) term_id
[terms]=
array(2) {
[0]=
int(47)
[1]=
int(834)
}
[operator]=
string(2) IN
}
}
[wc_query]=
string(13) product_query
[posts_per_page]=
int(12)
[post_type]=
string(7) product
}
[tax_query]=
object(WP_Tax_Query)#4683 (6) {
[queries]=
array(1) {
[0]=
array(5) {
[taxonomy]=
string(11) product_cat
[terms]=
array(1) {
[0]=
string(56) slug-of-the-category
}
[field]=
string(4) slug
[operator]=
string(2) IN
[include_children]=
bool(true)
}
}
[relation]=
string(3) AND
[table_aliases:protected]=
array(0) {
}
[queried_terms]=
array(1) {
[product_cat]=
array(2) {
[terms]=
array(1) {
[0]=
string(56) slug-of-the-category
}
[field]=
string(4) slug
}
}
[primary_table]=
NULL
[primary_id_column]=
NULL
}
[meta_query]=
bool(false)
[date_query]=
bool(false)
[queried_object]=
object(WP_Term)#4649 (10) {
[term_id]=
int(1059)
[name]=
string(59) Name of the category
[slug]=
string(56) slug-of-the-category
[term_group]=
int(0)
[term_taxonomy_id]=
int(1059)
[taxonomy]=
string(11) product_cat
[description]=
string(0)
[parent]=
int(416)
[count]=
int(0)
[filter]=
string(3) raw
}
[queried_object_id]=
int(1059)
[post_count]=
int(0)
[current_post]=
int(-1)
[in_the_loop]=
bool(false)
[comment_count]=
int(0)
[current_comment]=
int(-1)
[found_posts]=
int(0)
[max_num_pages]=
int(0)
[max_num_comment_pages]=
int(0)
[is_single]=
bool(false)
[is_preview]=
bool(false)
[is_page]=
bool(false)
[is_archive]=
bool(true)
[is_date]=
bool(false)
[is_year]=
bool(false)
[is_month]=
bool(false)
[is_day]=
bool(false)
[is_time]=
bool(false)
[is_author]=
bool(false)
[is_category]=
bool(false)
[is_tag]=
bool(false)
[is_tax]=
bool(true)
[is_search]=
bool(false)
[is_feed]=
bool(false)
[is_comment_feed]=
bool(false)
[is_trackback]=
bool(false)
[is_home]=
bool(false)
[is_privacy_policy]=
bool(false)
[is_404]=
bool(false)
[is_embed]=
bool(false)
[is_paged]=
bool(false)
[is_admin]=
bool(false)
[is_attachment]=
bool(false)
[is_singular]=
bool(false)
[is_robots]=
bool(false)
[is_favicon]=
bool(false)
[is_posts_page]=
bool(false)
[is_post_type_archive]=
bool(false)
[query_vars_hash:WP_Query:private]=
string(32) 0cab94343472426f19caa925968f6373
[query_vars_changed:WP_Query:private]=
bool(false)
[thumbnails_cached]=
bool(false)
[stopwords:WP_Query:private]=
NULL
[compat_fields:WP_Query:private]=
array(2) {
[0]=
string(15) query_vars_hash
[1]=
string(18) query_vars_changed
}
[compat_methods:WP_Query:private]=
array(2) {
[0]=
string(16) init_query_flags
[1]=
string(15) parse_tax_query
}
}
This is the POC filter:
function a3_include_filtered_ctgs($query)
{
if (!isset($_GET['filterCtg'])) return;
if ($query-is_main_query() !is_admin()) {
var_dump($query-get('tax_query'));
$taxquery = //$query-get('tax_query');
array(
array(
'taxonomy' = 'product_cat',
'field' = 'term_id',
'terms' = array_map(function ($id) {
return (int) $id;
}, explode(,, $_GET['filterCtg'])),
'operator' = 'IN'
)
);
if ($query-get('suppress_filters')) {
$query-set('suppress_filters', false);
}
$query-set('tax_query', $taxquery);
var_dump($query);
}
}
if (A3_DEBUG === true) {
add_action('pre_get_posts', 'a3_include_filtered_ctgs');
}
Topic tax-query pre-get-posts Wordpress
Category Web