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

Facet query var helper doesn't support 3+ terms #82

Open
mboynes opened this issue Mar 1, 2017 · 0 comments
Open

Facet query var helper doesn't support 3+ terms #82

mboynes opened this issue Mar 1, 2017 · 0 comments
Assignees
Labels

Comments

@mboynes
Copy link
Contributor

mboynes commented Mar 1, 2017

Basic example

add_filter( 'sp_search_wp_query_args', function( $wp_args ) {
	$wp_args['facets'] = [
		'Category' => [
			'type' => 'taxonomy',
			'taxonomy' => 'category',
			'count' => 10,
		],
	];

	return $wp_args;
} );

When outputting the facets, e.g. as links, the query_vars entry in the processed facet data contains the query vars needed to generate a link to filter the results:

$facet_data = SP_Integration()->search_obj->get_facet_data();
if ( ! empty( $facet_data['Category']['items'] ) ) :
	foreach ( $facet_data['Category']['items'] as $cat ) :
		?>
		<li>
			<a href="<?php echo esc_url( add_query_arg( $cat['query_vars'] ) ) ?>">
				<?php echo esc_html( $cat['name'] ) ?>
			</a>
		</li>
		<?php
	endforeach;
endif;

If you run a search and click a category, this works great. If you click a second category, it still works great. If you click a third category, the second is replaced with the third.

Problem and Proposed Solution

This is a problem with core, where core replaces the query var value with only the first in the list.

To fix this, we should be using $GLOBALS['wp_query']->tax_query->queries. It's important to note that union queries (?taxonomy=term1+term2) will add multiple arrays to $GLOBALS['wp_query']->tax_query->queries, whereas intersection queries (?taxonomy=term1,term2) will add multiple terms a single array entry. Here's a var_export() dump illustrating this:

?s=markup&category_name=antiquarianism,aciform
$GLOBALS['wp_query']->tax_query->queries
array (
  0 => 
  array (
    'taxonomy' => 'category',
    'terms' => 
    array (
      0 => 'antiquarianism',
      1 => 'aciform',
    ),
    'field' => 'slug',
    'operator' => 'IN',
    'include_children' => true,
  ),
)

‌‌?s=markup&category_name=antiquarianism+aciform
$GLOBALS['wp_query']->tax_query->queries
array (
  0 => 
  array (
    'taxonomy' => 'category',
    'terms' => 
    array (
      0 => 'antiquarianism',
    ),
    'field' => 'slug',
    'operator' => 'IN',
    'include_children' => true,
  ),
  1 => 
  array (
    'taxonomy' => 'category',
    'terms' => 
    array (
      0 => 'aciform',
    ),
    'field' => 'slug',
    'operator' => 'IN',
    'include_children' => true,
  ),
)

In addition, it looks like we're not fully accounting for union queries when building the query var arrays in the facet data. Relevant code in core to mimic for consistency.

@mboynes mboynes self-assigned this Mar 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant