Skip to content

Clearing search does nothing on an inherited-query Search field (query-s vs s mismatch) #50

Description

@peroks

Summary

When a core/search block is nested inside a core/query block with "inherit": true (the typical setup for a dedicated Search Results page — the block reads/filters the site's main query), clearing the search field does nothing: the displayed results/title never update, and the URL doesn't change.

Root cause

render_block_search() (inc/namespace.php) always names this field's query var query-s for the inherited case:

$query_var = empty( $instance->context['query']['inherit'] )
	? sprintf( 'query-%d-s', $instance->context['queryId'] ?? 0 )
	: 'query-s';

But the bundled client-side action (src/taxonomy/view.js's updateURL()) has a special case that only takes effect when the field's name is literally s:

const updateURL = async ( action, value, name ) => {
	const url = new URL( action );
	if ( value || name === 's' ) {
		url.searchParams.set( name, value );
	} else {
		url.searchParams.delete( name );
	}
	const { actions } = await import( '@wordpress/interactivity-router' );
	await actions.navigate( url.toString() );
};

Since the inherited case is actually named query-s, clearing the field (an empty value) always falls into the delete branch — deleting a query-s key that was never present in the URL to begin with (a page reached via a native ?s= link — e.g. any ordinary sitewide search box — has the real term in WordPress's own native s param, since that's what makes is_search()/routing resolve to the search results template at all). The resulting "new" URL is identical to the current one, so actions.navigate() effectively does nothing — no fetch, no update.

Reproduction

  1. Add a core/search block inside a core/query block with query.inherit: true, on a real Search Results template (search.html).
  2. Land on the results page via a native ?s=term URL (e.g. a plain, non-block search form elsewhere on the site).
  3. Clear the search field via the wired input (or type a value then clear it again).
  4. Expected: results/title update to reflect the empty search. Actual: nothing changes — same term, same results, same URL.

Suggested fix

Either:

  • Name the inherited-query field literally s instead of query-s in render_block_search() (matches what updateURL()'s own special case already assumes), or
  • Change updateURL()'s special case to match whatever name the PHP side actually computes for the inherited query, rather than hardcoding 's'.

We worked around this on our end with a theme-side render_block_core/search filter that renames the field for the is_search() case specifically, but it'd be good to fix at the source so other consumers don't have to do the same.

Environment

  • humanmade/query-filter v0.3.2
  • WordPress 7.0.2, PHP 8.4

Happy to open a PR if that's useful.

Disclosure

This issue was AI generated based on the findings in a long debugging session.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions