Skip to content

Commit cac5df0

Browse files
committed
Address review feedback on block search
1 parent b7ce73b commit cac5df0

3 files changed

Lines changed: 711 additions & 217 deletions

File tree

‎README.md‎

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ wp block
2424
# List all registered block types
2525
$ wp block type list
2626

27-
# Find posts that use a rounded block style
28-
$ wp block search --style=rounded
27+
# Find posts by block usage
28+
$ wp block search --block=core/button
2929

3030
# Get a specific block pattern
3131
$ wp block pattern get my-theme/hero
@@ -46,24 +46,30 @@ wp block
4646
Searches posts for block usage.
4747

4848
~~~
49-
wp block search [--block=<block-name>] [--block-namespace=<block-namespace>] [--style=<style-name>] [--pattern=<pattern-name>] [--pattern-namespace=<pattern-namespace>] [--synced-pattern=<post-id>] [--<field>=<value>] [--field=<field>] [--fields=<fields>] [--format=<format>]
49+
wp block search [--block=<block-name>] [--block-namespace=<block-namespace>] [--style=<style-name>] [--pattern=<pattern-name>] [--pattern-namespace=<pattern-namespace>] [--synced-pattern=<post-id>] [--limit=<limit>] [--<field>=<value>] [--field=<field>] [--fields=<fields>] [--format=<format>]
5050
~~~
5151

5252
Returns matching posts where the requested block appears anywhere in the
5353
parsed block tree, including nested blocks.
5454

55-
To reduce unnecessary parsing on large datasets, the command first applies a coarse `post_content` prefilter when a safe marker is available, then confirms matches by parsing blocks.
55+
To reduce unnecessary parsing on large datasets, the command first applies a
56+
coarse `post_content` prefilter when a safe marker is available, then
57+
confirms matches by parsing blocks. Candidate posts are loaded in batches to
58+
keep memory usage low. A plain `--block` search skips parsing entirely unless
59+
the `occurrences` field is requested.
5660

57-
At least one search filter is required: `--block`, `--block-namespace`, `--style`, `--pattern`, `--pattern-namespace`, or `--synced-pattern`.
61+
At least one search filter is required: `--block`, `--block-namespace`,
62+
`--style`, `--pattern`, `--pattern-namespace`, or `--synced-pattern`.
5863

59-
Pattern filters match embedded block metadata stored in `metadata.patternName`.
64+
Pattern filters match only blocks whose own `metadata.patternName` matches.
6065

61-
The `--synced-pattern` filter matches reusable block references by synced pattern post ID.
66+
The `--synced-pattern` filter matches reusable block references by synced
67+
pattern post ID, and cannot be combined with `--block` or `--block-namespace`.
6268

6369
**OPTIONS**
6470

6571
[--block=<block-name>]
66-
Block type name to search for (for example, 'core/paragraph').
72+
Block type name to search for (for example, 'core/paragraph'). A name without a namespace is treated as a core block.
6773

6874
[--block-namespace=<block-namespace>]
6975
Limit matches to blocks within a specific namespace (for example, 'core').
@@ -80,12 +86,23 @@ The `--synced-pattern` filter matches reusable block references by synced patter
8086
[--synced-pattern=<post-id>]
8187
Limit matches to reusable block references for a specific synced pattern post ID.
8288

83-
Note: `--block` and `--block-namespace` are mutually exclusive. `--pattern` and `--pattern-namespace` are also mutually exclusive.
89+
[--limit=<limit>]
90+
Stop after this many matching posts, in query order (post ID ascending unless
91+
`--orderby` is given). Unlike `--posts_per_page`, which limits the candidate
92+
posts examined, this counts matches. Cannot be combined with `--paged`.
8493

8594
[--<field>=<value>]
86-
One or more args to pass to WP_Query.
95+
One or more args to pass to WP_Query. The default `post_type` is `any`,
96+
which excludes `wp_block` and other post types that are not searchable.
97+
Pass `--post_type=wp_block` to search them explicitly.
98+
99+
Results are complete by default; candidate posts are processed in batches
100+
internally. Pass `--posts_per_page=<n>` and `--paged=<n>` to search only
101+
that window of candidate posts.
87102

88-
Use native WP_Query pagination args `--posts_per_page=<n>` and `--paged=<n>` to scan large sites in smaller batches. These args limit the candidate posts examined for a given run; they do not add pagination metadata to the command output. Results in each page may not be equal and empty response isn't guarantee that next page won't have results.
103+
Content inside synced patterns is not searched through `core/block`
104+
references, so a block that only exists inside a synced pattern is not
105+
found in the posts that use that pattern.
89106

90107
[--field=<field>]
91108
Prints the value of a single field for each matching post.
@@ -125,46 +142,55 @@ These fields are optionally available:
125142
**EXAMPLES**
126143

127144
# Find posts using the paragraph block.
128-
$ wp block search --block=core/paragraph
145+
$ wp block search --block=core/paragraph
146+
147+
# Find any post type posts using the heading block.
148+
$ wp block search --block=core/heading --post_type=any
129149

130150
# Find posts using any block in namespace like 'core'.
131151
$ wp block search --block-namespace=core
132152

133153
# Find posts using any blocks with block style of 'rounded'.
134-
$ wp block search --style=rounded
154+
$ wp block search --style=rounded
155+
156+
# Find posts using certain block with a specific style.
157+
$ wp block search --block=core/image --style=rounded
158+
159+
# Search a namespace with a specific style.
160+
$ wp block search --block-namespace=core --style=rounded
135161

136-
# Combine namespace and style filters.
137-
$ wp block search --block-namespace=core --style=rounded
162+
# Search published pages for rounded images.
163+
$ wp block search --block=core/image --style=rounded --post_type=page --post_status=publish
138164

139-
# Find posts using blocks embedded from a specific pattern.
140-
$ wp block search --pattern=twentytwentyfive/event-rsvp
165+
# Find posts using blocks embedded from a specific pattern.
166+
$ wp block search --pattern=twentytwentyfive/event-rsvp
141167

142-
# Find posts using blocks embedded from patterns in a specific namespace.
143-
$ wp block search --pattern-namespace=twentytwentyfive
168+
# Find posts using blocks from patterns in a specific namespace.
169+
$ wp block search --pattern-namespace=twentytwentyfive
144170

145-
# Find posts using a specific synced pattern.
146-
$ wp block search --synced-pattern=123
171+
# Find posts using a specific synced pattern.
172+
$ wp block search --synced-pattern=123
147173

148-
# Combine pattern and block filters.
149-
$ wp block search --pattern-namespace=twentytwentyfive --block=core/image
174+
# Show selected fields as JSON for further processing.
175+
$ wp block search --block=core/heading --post_status=publish --fields=ID,post_type,occurrences --format=json
150176

151-
# Limit search to published pages and show selected fields as JSON.
152-
$ wp block search --block=core/image --post_type=page --post_status=publish --fields=ID,post_title,occurrences --format=json
177+
# Limit the candidate posts scanned with a native query argument.
178+
$ wp block search --block=core/paragraph --showposts=50 --format=ids
153179

154-
# Limit the candidate posts scanned with a native query argument.
155-
$ wp block search --block=core/paragraph --showposts=50 --format=ids
180+
# Search only the second window of 1000 candidate posts.
181+
$ wp block search --block=core/paragraph --posts_per_page=1000 --paged=2 --format=ids
156182

157-
# Scan the second batch of 100 candidate posts without changing output format.
158-
$ wp block search --block=core/paragraph --posts_per_page=100 --paged=2 --format=ids
183+
# Restrict search to specific posts and return the count.
184+
$ wp block search --style=rounded --post__in=21,42,84 --format=count
159185

160-
# Restrict search to specific posts and return the match count.
161-
$ wp block search --style=rounded --post__in=21,42,84 --format=count
186+
# Return the first 20 posts using the paragraph block.
187+
$ wp block search --block=core/paragraph --limit=20 --format=ids
162188

163189
# Return only matching post IDs.
164-
$ wp block search --block=core/paragraph --format=ids
190+
$ wp block search --block=core/paragraph --format=ids
165191

166192
# Return count of matching posts.
167-
$ wp block search --block=core/heading --format=count
193+
$ wp block search --block=core/heading --format=count
168194

169195

170196

0 commit comments

Comments
 (0)