diff --git a/CHANGELOG.md b/CHANGELOG.md index cf731b3..b27b969 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +## v2.3.1 - 2024.08.19 + +### 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)) + +### Added + +- Add a CLAUDE.md file ([c28b253](https://github.com/studiometa/wp-toolkit/commit/c28b253)) + +## v2.3.0 - 2024.08.14 + ### Added - Add support for custom taxonomies filtering ([#42](https://github.com/studiometa/wp-toolkit/issues/42), [#43](https://github.com/studiometa/wp-toolkit/pull/43), [ae541b5](https://github.com/studiometa/wp-toolkit/commit/ae541b5)) diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..7f2b2ea --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,14 @@ +# WordPress Toolkit by Studio Meta + +## Development commands + +- `ddev start` to start the DDEV project where the PHPUnit tests will be run +- `ddev exec composer test` to run the PHPUnit tests +- `composer lint:style` to check for code quality with PHPCS +- `composer fix:style` to fix fixable errors reported by PHPCS +- `composer lint:static` to do static analysis with PHPStan + +## Guidelines + +- Follow existing patterns +- Follow modern PHP features and standard diff --git a/src/Managers/FacetsManager.php b/src/Managers/FacetsManager.php index 5dc74d7..dc778bb 100644 --- a/src/Managers/FacetsManager.php +++ b/src/Managers/FacetsManager.php @@ -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; } diff --git a/tests/Managers/FacetsManagerTest.php b/tests/Managers/FacetsManagerTest.php index 25e6236..72603ac 100644 --- a/tests/Managers/FacetsManagerTest.php +++ b/tests/Managers/FacetsManagerTest.php @@ -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'])); + } }