Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Fixed

- Fix facets applying on non-main queries ([#49](https://github.com/studiometa/wp-toolkit/issues/49), [#50](https://github.com/studiometa/wp-toolkit/pull/50), [9c098c7](https://github.com/studiometa/wp-toolkit/commit/9c098c7))

## v2.3.0 - 2024.08.14

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/Managers/FacetsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function run()
*/
public function add_facets_to_query(WP_Query &$query): void
{
if (!is_array($this->facets)) {
if (!is_array($this->facets) || !$query->is_main_query()) {
return;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Managers/FacetsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,21 @@ public function test_it_should_handle_array_values_in_facets()
$this->assertEquals(['slug1', 'slug2'], $tax_query[0]['terms']);
$this->assertEquals('slug', $tax_query[0]['field']);
}

public function test_it_should_not_add_facets_to_non_main_query()
{
request()->query->set('facets', ['cat' => 1]);
$manager = new FacetsManager();
$manager->run();

// Create a non-main query
$secondary_query = new WP_Query();
$secondary_query->init();

// Manually call the method to simulate what happens on pre_get_posts
$manager->add_facets_to_query($secondary_query);

// The facets should not be applied to the secondary query
$this->assertFalse(isset($secondary_query->query_vars['cat']));
}
}
Loading