Skip to content

Core Data: Add an exclude option to fetchLinkSuggestions - #83084

Closed
jeryj wants to merge 9 commits into
trunkfrom
feat/link-suggestions-multiple-types
Closed

jeryj wants to merge 9 commits into
trunkfrom
feat/link-suggestions-multiple-types

Conversation

@jeryj

@jeryj jeryj commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

What?

Adds an exclude option to __experimentalFetchLinkSuggestions:

fetchLinkSuggestions( 'home', { exclude: [ 'attachment' ] } );

Why?

An unscoped search covers every type. In #82985, we want to search all types except unlikely ones to be included for navigation links (i.e., attachments)

How?

Add an exclude: [] prop and use it to exclude our type.

const isSearched = ( searchType: SearchType ) =>
	( ! type || type === searchType ) && ! exclude?.includes( searchType );

Testing Instructions

The change is to a JavaScript API, so this is exercised from the browser console. Use a site with a media library.

  1. Open any post in the editor and open the browser console.
  2. Search for a word that matches some attachments:
    const fetch = wp.coreData.__experimentalFetchLinkSuggestions;
    ( await fetch( 'image', { perPage: 20 } ) ).map( r => `${ r.kind }: ${ r.title }` );
    Note how many results are media. On the site used above, 5 of 7.
  3. Exclude them:
    ( await fetch( 'image', { perPage: 20, exclude: [ 'attachment' ] } ) )
        .map( r => `${ r.kind }: ${ r.title }` );
    No attachments, and the room they took is now free for other results.
  4. Confirm a narrowed search is unaffected, since it was never going to search attachments:
    ( await fetch( '', { type: 'term', exclude: [ 'attachment' ] } ) ).length;
    The same as without exclude.

To see the Navigation case from the "Why" section: add a Navigation block, use the appender, choose Add block → Custom Link, and search "image".

Automated coverage

npm run test:unit:vitest -- --run packages/core-data/src/fetch

🤖 Generated with Claude Code

`type` selected one entity type or, when omitted, all of them. A caller
that wants posts and terms but not attachments had no way to say so, and
had to discard the attachments afterwards. That is not equivalent: the
merged results are sliced to `perPage` before the caller sees them, so
attachments can consume the page and be thrown away, leaving fewer
suggestions than asked for.

`type` now also accepts an array. `subtype` is ignored for a multi-type
search, since it belongs to a single type and cannot be applied
unambiguously.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BLn4hqAvfFyNGLLgsSkCcv
@jeryj
jeryj requested a review from Mamaduka as a code owner September 17, 2026 13:50
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BLn4hqAvfFyNGLLgsSkCcv
@github-actions github-actions Bot added the [Package] Core data /packages/core-data label Sep 17, 2026
@github-actions

github-actions Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

📦 Bundle size

Size Change: +12 B (0%)

Total Size: 8.21 MB

📦 View Changed
Filename Size Change
build/scripts/core-data/index.min.js 37.5 kB +12 B (+0.03%)

a581f8c Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric c04fc21 trunk % Change
timeToFirstByte 57.05 ms +9.64% -5.7% 54.25 ms +12.72% -1.38% 5.16%
largestContentfulPaint 100 ms +0% -4% 96 ms +0% -8.33% 4.17%
lcpMinusTtfb 42.4 ms +3.77% -18.51% 34.65 ms +21.65% -9.09% 22.37%
wpBeforeTemplate 27.53 ms +19.65% -1.82% 27.31 ms +17.76% -1.83% 0.81%
wpTemplate 23.78 ms +4.96% -2.27% 23.65 ms +2.54% -3.38% 0.55%
wpTotal 53.53 ms +8.85% -6.11% 50.71 ms +12.92% -1.42% 5.56%
wpMemoryUsage 7.57 MB +0% -0% 7.53 MB +0% -0% 0.46%
wpDbQueries 17 +0% -0% 17 +0% -0% 0%

front-end-classic-theme

Metric c04fc21 trunk % Change
timeToFirstByte 46.15 ms +5.74% -1.3% 45.1 ms +10.31% -0.55% 2.33%
largestContentfulPaint 100 ms +2% -0% 100 ms +2% -4% 0%
lcpMinusTtfb 54 ms +1.2% -0.65% 53.8 ms +2.23% -4.74% 0.37%
wpBeforeTemplate 25.76 ms +4.23% -2.37% 25.12 ms +4.02% -1.23% 2.55%
wpTemplate 17.45 ms +2.23% -1.09% 17.38 ms +3.8% -1.5% 0.4%
wpTotal 43.3 ms +5.54% -1.57% 42.24 ms +10.46% -0.45% 2.51%
wpMemoryUsage 6.19 MB +0% -0% 6.15 MB +0% -0% 0.58%
wpDbQueries 14 +0% -0% 14 +0% -0% 0%

media-processing

Metric c04fc21 trunk % Change
mediaProcessingJpeg 396.85 ms +1.09% -0.92% 394.16 ms +0.73% -0.89% 0.68%
mediaProcessingAvif 6043.51 ms +0.09% -0.11% 6013.21 ms +0.1% -0.13% 0.5%
mediaProcessingJpegToAvif 4168.64 ms +0.2% -0.37% 4151 ms +0.16% -0.06% 0.42%

media-upload

Metric c04fc21 trunk % Change
jpegUploadProcessing 1433.73 ms +35.21% -1.79% 1409.58 ms +1.49% -1.32% 1.71%
pngUploadProcessing 208.47 ms +7.14% -8.9% 195.7 ms +11.28% -2.58% 6.53%
largeJpegUploadProcessing 1409.63 ms +0.46% -0.58% 1408.06 ms +0.68% -0.62% 0.11%
multipleImageUploadProcessing 1546.09 ms +2.37% -1.42% 1542.83 ms +1.26% -0.81% 0.21%

post-editor

Metric c04fc21 trunk % Change
serverResponse 527.83 ms +3.11% -4.77% 504.95 ms +7.59% -3.61% 4.53%
firstPaint 227 ms +15.76% -11.27% 234.99 ms +24.62% -4.94% -3.4%
domContentLoaded 1127.96 ms +0.87% -1.7% 1099.74 ms +1.5% -3.73% 2.57%
loaded 1129.39 ms +0.85% -1.71% 1101.08 ms +1.5% -3.71% 2.57%
firstContentfulPaint 453.47 ms +1.81% -4.54% 442.35 ms +4.04% -1.7% 2.51%
firstBlock 3337.47 ms +0.49% -1.16% 3307.36 ms +1.91% -1.61% 0.91%
type 23 ms +1.3% -4% 19.82 ms +3.94% -3.53% 16.04%
typeWithoutInspector 21.1 ms +4.64% -5.31% 21.62 ms +2.36% -3.52% -2.41%
typeWithTopToolbar 26.39 ms +1.48% -4.62% 26.83 ms +3.95% -8.39% -1.64%
typeContainer 9.22 ms +6.62% -3.15% 9.35 ms +8.13% -4.17% -1.39%
focus 78.02 ms +5.54% -7.51% 84.89 ms +3.2% -4.32% -8.09%
firstFocus 220.16 ms +0% -0% 224.62 ms +0% -0% -1.99%
selectAll 523.97 ms +0.96% -1.62% 547.88 ms +4.42% -1.36% -4.36%
listViewOpen 70.61 ms +11.16% -3.65% 74.7 ms +3.88% -4.02% -5.48%
inserterOpen 24.31 ms +14.69% -8.27% 25.14 ms +5.93% -8.71% -3.3%
inserterHover 3.11 ms +19.29% -8.04% 3.54 ms +10.73% -7.06% -12.15%
inserterSearch 8.8 ms +8.3% -12.61% 8.77 ms +2.28% -4.79% 0.34%
loadPatterns 669.62 ms +5.13% -5.86% 659.65 ms +2.87% -3.05% 1.51%
wpTotal 516.94 ms +3.35% -4.71% 494.79 ms +7.83% -3.62% 4.48%
wpMemoryUsage 13.10 MB +0% -0% 13.06 MB +0% -0% 0.28%
wpDbQueries 54 +0% -1.85% 54 +0% -0% 0%

site-editor

Metric c04fc21 trunk % Change
serverResponse 333 ms +1.21% -4.66% 325.05 ms +4.36% -4.73% 2.45%
firstPaint 225.4 ms +47.95% -14.53% 199.66 ms +78.25% -13.04% 12.89%
domContentLoaded 881.29 ms +2.52% -2.09% 871.38 ms +1.99% -1.9% 1.14%
loaded 882.41 ms +2.52% -2.1% 872.51 ms +1.99% -1.9% 1.13%
firstContentfulPaint 368.11 ms +3.5% -2.86% 362.01 ms +1.71% -2.08% 1.69%
firstBlock 3147.95 ms +1.97% -1.34% 3105.49 ms +1.65% -0.43% 1.37%
type 19.5 ms +1.49% -3.74% 19 ms +6% -1.68% 2.63%
navigate 99.83 ms +26.4% -4.4% 115.42 ms +3.76% -4.88% -13.51%
loadPatterns 1122.53 ms +18.8% -2.75% 1083.71 ms +3.07% -5.74% 3.58%
loadPages 1127.1 ms +3.21% -0.75% 1134.87 ms +2.07% -1.37% -0.68%
wpTotal 324.65 ms +1.14% -4.83% 316.51 ms +4.49% -4.81% 2.57%
wpMemoryUsage 12.06 MB +0% -0% 12.02 MB +0% -0% 0.31%
wpDbQueries 44 +0% -2.27% 44 +0% -2.27% 0%

a581f8c Run

@github-actions

github-actions Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: jeryj <jeryj@git.wordpress.org>
Co-authored-by: scruffian <scruffian@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@jeryj jeryj added [Type] Enhancement A suggestion for improvement. [Feature] Link Editing Link components (LinkControl, URLInput) and integrations (RichText link formatting) labels Sep 17, 2026
jeryj and others added 7 commits September 17, 2026 11:05
"Cannot be applied unambiguously" undersold it. Validation accepts any
registered post type or taxonomy name, so a post type slug reaches the
term handler without being rejected and returns nonsense:
`type=term&subtype=page` leaks the private `wp_template_part_area`
taxonomy and returns more rows than an unscoped term search.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BLn4hqAvfFyNGLLgsSkCcv
Two tests did not describe the option. Adds cases for an array of one
behaving like the bare type, subtype surviving that case, types outside
the array not being searched, attachments when asked for, disablePostFormats
still winning over an explicit request, and an empty array searching
nothing.

Writing them showed the subtype guard was inconsistent: it keyed on
Array.isArray, so `[ 'post' ]` dropped a subtype while `'post'` kept it,
even though both search exactly one type. It now keys on how many types
were requested, since one type is unambiguous however it is spelled.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BLn4hqAvfFyNGLLgsSkCcv
Dropping subtype for a multi-type search meant "pages and categories"
could not be expressed at all, only "every post type and every taxonomy".
Sending one subtype to every handler is not an option either: an
unmatched subtype does not degrade, it corrupts. On a test site the term
handler went from 13 results to 16, leaking the private
`wp_template_part_area` and `post_format` taxonomies, while the post
handler silently dropped every page.

So a subtype travels with its type:

    type: [ { type: 'post', subtype: 'page' }, { type: 'term' } ]

Every accepted shape normalizes to the same internal list, so the older
forms are unchanged: a bare type with a top level subtype, and an array
of bare types. The top level subtype applies only to a single type given
on its own, since in the array form each entry carries its own.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BLn4hqAvfFyNGLLgsSkCcv
Mapping the results to `kind` produced [ 'post-type', 'taxonomy',
'taxonomy' ], which reads like a duplicate until you go and check the
mock. It is one result from the post search and two from the term
search. Assert the titles alongside the kinds so the test says that.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BLn4hqAvfFyNGLLgsSkCcv
Allowing both `[ 'post' ]` and `[ { type: 'post' } ]` meant answering
whether the top level subtype applied to a bare entry, which is the
ambiguity this shape existed to remove. An array now takes entries only,
and the normalization is two lines: one type becomes one entry carrying
the top level subtype, a list is already entries.

Also covers several subtypes for one type, which the REST parameter has
always accepted and nothing tested. addQueryArgs indexes array values, so
the mock needed the subtype[0] form.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BLn4hqAvfFyNGLLgsSkCcv
The array of types grew into entries carrying their own subtypes, which
raised a question at every step: whether a bare entry inherited the top
level subtype, what an empty array meant, and whether `type` was still an
honest name for a list of search descriptions.

None of that was needed. The problem was never selecting types, it was
that a caller cannot use every type and had to discard results after the
merge had already cut them to `perPage`. `exclude` says exactly that and
nothing more, leaves `type` and `subtype` untouched, and takes a new
handler into account by default rather than being a list to keep updated.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BLn4hqAvfFyNGLLgsSkCcv
`subtype` was widened to accept an array while `type` could hold entries
that carried their own. Nothing needs that now, so it goes back to a
string.

`SearchType` stays, since `exclude` needs the union named and repeating a
four member union twice is worse, but it is no longer exported. Naming it
is an implementation detail, not a new public type.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BLn4hqAvfFyNGLLgsSkCcv
@jeryj jeryj changed the title Core Data: Let fetchLinkSuggestions search several types Core Data: Add an exclude option to fetchLinkSuggestions Sep 17, 2026
* Filters by search type.
*/
type?: 'attachment' | 'post' | 'term' | 'post-format';
type?: SearchType;

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.

What if this was an array like ['attachment' | 'post' | 'term' | 'post-format'] so if you want to exclude one you just remove it from the array?

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.

We could do that, but then what do we do about subType? If you pass an array of types and a string for subType, does the subType get passed to each query? Excluding was the safest next step, but I think this whole API is a bit busted. For example, the perPage and page don't work correctly right now.

If you pass 20 perPage results for page 2, what you're doing is a page 2 search for all of the types (four diffrent types; 'attachment' | 'post' | 'term' | 'post-format') and then receiving the 20 best matches of the 80 results back. When you request page 3, you're getting the page 3 search results of each of them, but only returning 20 out of the possible pool of 80 back. Each page, you're actually losing 60 results each time you move forward, so it's already a little fuzzy. Maybe we should rethink how this whole API works and not add an exclusion for now.

That said, maybe the above is the better approach. Do we really want to re-search the worst results from our search pool again?

I'm wary of making any big changes here right now, as each time I start making a change I run into something that makes me rethink it. Also, I think what is being saved by excluding types isn't really that essential to #82985.

@jeryj

jeryj commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

I'm going to close this PR for now. I'm unsure if it's the right move.

@jeryj jeryj closed this Sep 18, 2026
@jeryj jeryj reopened this Sep 21, 2026
jeryj added a commit that referenced this pull request Sep 21, 2026
Adds the `exclude` option from #83084 to `fetchLinkSuggestions`, and uses
it from the Navigation Link UI.

An attachment is a file rather than a destination, and a post format is a
way of styling a post rather than something to navigate to. Ranking them
last keeps them out of the way, but they are still fetched and still take
room in a list that is cut to `perPage` before the caller sees it.
Excluding them frees that room for results a Navigation can use.

The ranking stays, because it still applies to every other `LinkControl`
in the editor, none of which excludes anything.

A Post Format Link searches post formats, so it keeps them: the exclusion
drops whichever types the link does not search for itself.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VbpgaY282xpLjwA8aQv3x9
jeryj added a commit that referenced this pull request Sep 22, 2026
…y type

Replaces the exclusion added for Navigation with a ranking that keeps
every type reachable, and makes the order a caller's to choose.

`DEFAULT_TYPE_ORDER` ranks pages, then categories, posts, tags,
attachments and post formats. `typeOrder` overrides it; a caller may name
every type, or only the ones it wants to lead with, and the rest keep
their default places below them. The Navigation Link UI names its own
type, so each link leads with the kind of thing it is.

Nothing is dropped. An attachment is reachable by typing its exact title,
which is the only signal that lifts a result past the rank of its type.
Beginning with the search term orders titles within a type but never
raises one type above another, so an attachment called "Coffee Cup Photo"
cannot pass a page called "Our Coffee".

Removes the `exclude` option, which no caller now uses; #83084 proposes it
separately.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VbpgaY282xpLjwA8aQv3x9
@jeryj

jeryj commented Sep 22, 2026

Copy link
Copy Markdown
Contributor Author

I think we still could include this, but I no longer think it's necessary if #83408 lands, which would introduce a preference for a specific type and #83407 which puts demotes attachments at the bottom of the search results.

@jeryj jeryj closed this Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Link Editing Link components (LinkControl, URLInput) and integrations (RichText link formatting) [Package] Core data /packages/core-data [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants