Skip to content

Fix undefined array key warning in Live Search when no category is selected#2871

Open
SAY-5 wants to merge 1 commit into
Codeinwp:masterfrom
SAY-5:fix/live-search-undefined-cat-key
Open

Fix undefined array key warning in Live Search when no category is selected#2871
SAY-5 wants to merge 1 commit into
Codeinwp:masterfrom
SAY-5:fix/live-search-undefined-cat-key

Conversation

@SAY-5

@SAY-5 SAY-5 commented Jun 18, 2026

Copy link
Copy Markdown

Closes #2849.

Summary

When a Live Search block restricts results to post but no category is selected, otterSearchQuery['cat'] is never written to the block attributes, so render_blocks() accesses a missing key and PHP 8 emits Undefined array key "cat". Guarded the read in class-live-search.php with ?? '' so the absent key falls back to an empty data-cat attribute, matching how the surrounding post_type access is already protected.

Screenshots

Test instructions

  1. Add a Search block and enable Live Search.
  2. Set "Search in" to post and leave Post Category empty.
  3. View the page on the frontend; no PHP warning should appear and search works with no category filter.

Added tests/test-live-search-render.php, which activates a license, renders a post-only Live Search block without a cat key, and asserts the output contains data-cat="". It fails on the warning before the fix and passes after.


Checklist before the final review

  • Included E2E or unit tests for the changes in this PR.
  • Visual elements are not affected by independent changes.
  • It is at least compatible with the minimum WordPress version.
  • It loads additional script in frontend only if it is required.
  • Does not impact the Core Web Vitals.
  • In case of deprecation, old blocks are safely migrated.
  • It is usable in Widgets and FSE.
  • Copy/Paste is working if the attributes are modified.
  • PR is following the best practices

…lected

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
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.

Undefined array key "cat" warning in Live Search when no category is selected

1 participant