Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/facets tax query #2560

Merged
merged 12 commits into from
Feb 22, 2022
Next Next commit
Determine if there is an existing taxonomy query before facets inject…
… one.
  • Loading branch information
oscarssanchez committed Jan 18, 2022
commit 0a7a0c1090d689b1d2fb08bfd03f397402779368
31 changes: 25 additions & 6 deletions includes/classes/Feature/Facets/Facets.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,15 @@ public function facet_query( $query ) {
}

foreach ( $selected_filters['taxonomies'] as $taxonomy => $filter ) {
$tax_query[] = [
'taxonomy' => isset( $attribute_taxonomies[ $taxonomy ] ) ? $attribute_taxonomies[ $taxonomy ] : $taxonomy,
'field' => 'slug',
'terms' => array_keys( $filter['terms'] ),
'operator' => ( 'any' === $settings['match_type'] ) ? 'or' : 'and',
];
$tax = isset( $attribute_taxonomies[ $taxonomy ] ) ? $attribute_taxonomies[ $taxonomy ] : $taxonomy;
if ( ! $this->has_taxonomy_query( $tax_query, $tax ) ) {
$tax_query[] = [
'taxonomy' => $tax,
'field' => 'slug',
'terms' => array_keys( $filter['terms'] ),
'operator' => ( 'any' === $settings['match_type'] ) ? 'or' : 'and',
];
}
}

if ( ! empty( $selected_filters['taxonomies'] ) && 'any' === $settings['match_type'] ) {
Expand All @@ -339,6 +342,22 @@ public function facet_query( $query ) {
$query->set( 'tax_query', $tax_query );
}

/**
* Determine if we have a taxonomy query before attempting to add a new one for each filter.
*
* @param $tax_query array The current tax_query.
* @param $taxonomy string WP Taxonomy.
* @return bool true or false.
*/
function has_taxonomy_query( $tax_query, $taxonomy ) {
foreach ( $tax_query as $query ) {
if ( is_array( $query ) && $taxonomy === $query['taxonomy'] ) {
return true;
}
}
return false;
}

/**
* Hacky. Save aggregation data for later in a global
*
Expand Down