Skip to content

Instantly share code, notes, and snippets.

@markhowellsmead
Last active August 23, 2022 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markhowellsmead/86711ff842fcb9ed8b13d6218c6545c3 to your computer and use it in GitHub Desktop.
Save markhowellsmead/86711ff842fcb9ed8b13d6218c6545c3 to your computer and use it in GitHub Desktop.
Modify Woocommerce product block tax query
<?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');
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