-
-
Save markhowellsmead/86711ff842fcb9ed8b13d6218c6545c3 to your computer and use it in GitHub Desktop.
Modify Woocommerce product block tax query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function match_all_product_tags($query) | |
{ | |
if (!is_admin() && $query->get('post_type') === 'product') { | |
foreach ($query->query['tax_query'] as $key => $condition) { | |
if ($condition['taxonomy'] === 'product_tag' && count($condition['terms']) > 1) { | |
$new_tax_query = ['relation' => 'AND']; | |
foreach (array_values($condition['terms']) as $term_id) { | |
$new_tax_query[] = [ | |
'taxonomy' => 'product_tag', | |
'field' => 'term_id', | |
'terms' => $term_id | |
]; | |
} | |
unset($query->query['tax_query'][$key]); | |
$query->query['tax_query'][] = $new_tax_query; | |
$query->query['tax_query']['relation'] = 'AND'; | |
} | |
} | |
var_dump($query->query); | |
} | |
} | |
add_action('pre_get_posts', 'match_all_product_tags'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array | |
( | |
[post_type] => product | |
[post_status] => publish | |
[fields] => ids | |
[ignore_sticky_posts] => 1 | |
[no_found_rows] => | |
[orderby] => date ID | |
[order] => DESC | |
[meta_query] => Array | |
( | |
) | |
[tax_query] => Array | |
( | |
[1] => Array | |
( | |
[taxonomy] => product_visibility | |
[field] => term_taxonomy_id | |
[terms] => Array | |
( | |
[0] => 7 | |
) | |
[operator] => NOT IN | |
) | |
[2] => Array | |
( | |
[relation] => AND | |
[0] => Array | |
( | |
[taxonomy] => product_tag | |
[field] => term_id | |
[terms] => 19 | |
) | |
[1] => Array | |
( | |
[taxonomy] => product_tag | |
[field] => term_id | |
[terms] => 25 | |
) | |
) | |
[relation] => AND | |
) | |
[posts_per_page] => 9 | |
[meta_key] => | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment