Skip to content

Fix bug where search inputs shared state across query loops#45

Merged
kadamwhite merged 6 commits into
mainfrom
search-box-linkage
Jun 24, 2026
Merged

Fix bug where search inputs shared state across query loops#45
kadamwhite merged 6 commits into
mainfrom
search-box-linkage

Conversation

@kadamwhite

@kadamwhite kadamwhite commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

This resolves a bug where using a search input within multiple query loops incorrectly caused all search input's state to be tied together.

image

Reproduction:

  • Insert multiple query loops
  • Set each one to NOT inherit the main query, if needed
  • Add a search input within each query loop
  • View the rendered page
  • Insert a query into one query loop

Expected: only the selected search box would update; Actual: both update.

  • Manually set each query loop's query parameter separately, in the URL
  • Reload the page

Expected: Each query loop shows appropriate results, and each search box shows the respective correct term; Actual: The queries execute properly but once JS kicks in, the value of the first query loop is updated to match the second.

While testing, I realized there was a double-decoding issue in the custom sanitization around the search field. I've attempted to reimplement that to preserve the whitespace that #4 was intended to fix, but using core's own method to fix this doubled decoding issue.

The use of a global search context value meant that if multiple query
loops were all made searchable, the last rendered or updated would "take
over" all others. Moving search term scope to block context allows it to
be managed independently per query loop.
… input

Plus characters and certain % patterns would not be restored properly
with this logic, which imperfectly reimplemented parts of WP Core's
_sanitize_text_fields().
Base automatically changed from enhanced-query-filter-blocks-updated to main June 23, 2026 20:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an Interactivity bug where multiple Search blocks inside different Query Loop instances shared a single searchValue, causing all inputs to mirror each other after hydration. The PR scopes search state to each block’s Interactivity context and refactors the server-side value handling to avoid double-decoding while preserving user-visible whitespace.

Changes:

  • Scope searchValue to the block context (instead of shared store state) so multiple searchable query loops can coexist.
  • Replace custom search query sanitization with a helper that uses sanitize_text_field() while restoring surrounding whitespace.
  • Initialize each Search block’s Interactivity context (data-wp-context) with its own sanitized searchValue.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/taxonomy/view.js Switches search handling from global store state to per-block Interactivity context.
inc/namespace.php Adds a search sanitization helper and wires Search block rendering to use per-block context + the new sanitization.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread inc/namespace.php Outdated
Comment thread inc/namespace.php Outdated

@goldenapples goldenapples left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of these fixes make sense. I'd just make sure that you add two entries to the changelog after merging, because these are essentially two different fixes (or one fix and one new feature).

Comment thread inc/namespace.php
return '';
}

$sanitized = sanitize_text_field( $value );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One potential improvement:

Suggested change
$sanitized = sanitize_text_field( $value );
$sanitized = sanitize_textarea_field( $value );

would remove the whitespace collapsing inside search terms, so that you could search for things like "a b" with a double space...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! However in my testing this doesn't impact results or prevent you from typing multiple spaces when searching, so I propose we leave as-is since sanitize_text_field was the original method prior to https://github.com/humanmade/query-filter/pull/4/changes

@kadamwhite kadamwhite merged commit 9b156ef into main Jun 24, 2026
1 check passed
@kadamwhite kadamwhite deleted the search-box-linkage branch June 24, 2026 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants