If you’re building a WordPress site using its full-site editing features to create a blog, you’ll almost certainly be using the core Search block. While more advanced sites may reach for a third-party solution for their search needs, the core search block does more than it lets on. While on the surface, it looks like it offers just a little bit of visual customization, you can manually edit the block’s markup to add query params to do things like limit a search to a particular post type.
Add query params to the Search block
It’s not well-documented, but the search block accepts a variety of parameters under-the-hood that can’t be edited via the basic user interface. And to add an extra layer of difficulty, you can’t edit the Search block as HTML right in the block editor like you can many other blocks.
But like any other block, the Search block can be configured with raw JSON in its markup.
Here’s what the Search block’s basic markup looks like:
<!-- wp:search {"label":"Search","buttonText":"Search"} /-->Code language: HTML, XML (xml)
Super simple. You can get that by using the Copy tool, or hitting ⌘C on your keyboard. So first, configure everything you can about your Search block using the user interface, like positioning the button. Then Copy it to your clipboard, open your favorite editor, and Paste.
This part right here:
{"label":"Search","buttonText":"Search"}Code language: JSON / JSON with Comments (json)
is JSON, and one of the parameters it accepts is query. query accepts most of your typical WP_Query arguments in JSON format. So let’s say you’re adding a search bar to your blog, and intuitively, it should just search other Posts when used instead of also searching Pages or other post types. You can simply pass the post_type parameter as JSON to a new query argument:
<!-- wp:search {"label":"Search","buttonText":"Search","query":{"post_type":"post"}} /-->Code language: HTML, XML (xml)
In the block editor, create a new HTML block and paste that in. You’ve now got a Search block that only searches Posts. You can safely use the “Convert to Blocks” tool to make this look nice, but you won’t have any visual indication you’ve edited it, and you’ll need to use the Copy tool again to grab the code for further editing in the future.
Add other query parameters
Remember, this accepts all kinds of query parameters—anything you can append to a WordPress URL. I don’t have an exhaustive list after a cursory search, but things like author, tag, and even custom taxonomies are fair game.
Save your Search block for reuse
Without visual indication that you’ve edited this block, it’s nice to save it out as a pattern you can easily identify and reuse later as something separate from the original Search block. Right click the block in the block editor and use the “Create pattern” tool.
Why didn’t I know about this?
This is actually documented in the Search block’s block.json file, as are all parameters that any block accepts. But these aren’t well documented and you have to go digging into the search code.
Fortunately, there’s an effort underway to publicly document and automatically update block.json schema for every block.

Leave a Reply