Skip to content

Link builder arguments - 'wp_link_query_args' filter is ignored #31496

Open

Description

@designbuildtest

Description

Using the classic editor, it's possible to exclude post types and taxonomies from appearing in the link builder using:

function my_custom_link_query( $query ){
$query['post_type'] = array( 'post', 'pages', 'snippets' );
return $query;
}
add_filter( 'wp_link_query_args', 'my_custom_link_query' );

Gutenberg does not appear to support this same level of filtering of link builder arguments. All post types and taxonomies are available as link options by default.

Expected behaviour

Feature parity with classic editor enabling the filtering of link builder arguments.

Actual behaviour

All post types and taxonomies are available, even when filters are applied via wp_link_query_args

Screenshots

wp_link_query_args

Activity

  1. added
    [Feature] Link Editing
    [Type] Enhancement
    on May 5, 2021
  2. skorasaurus commented on May 5, 2021

    @skorasaurus
    Member

    Similar request at #22288 specifically for filtering taxonomies

  3. getdave commented on Sep 23, 2021

    @getdave
    Contributor

    I understand the motivation behind this request. Thank you for taking the time to raise it.

    Currently, the items shown the new Link UI are retrieved from the v2/search REST API endpoint.

    Unfortunately I don't think we can simply add in the wp_link_query_args into that API endpoint because it would be out of context.

    If we agree that adding backwards compatibility for this feature is required, then we might need to introduce a sort of "Proxy" endpoint for "Link search" (eg: WP_REST_Link_Search_Controller). We can then start to support filters such as wp_link_query_args whilst still proxying the ultimate request to the WP_REST_Search_Controller endpoint.

    I'd like to get some other opinions on this. Perhaps @spacedmonkey and @hellofromtonya would be able to advise further?

  4. ajtatum commented on Dec 8, 2022

    @ajtatum

    Without the ability to filter the results, this "feature" is useless on my site as a portion of my site is content aggregation (a custom post type) so whenever I try to link to my own content, it pretty much never comes up in the auto suggest... I have to manually insert the URL of the page. So, being able to filter the post type would be a game changer.

  5. msacadem commented on Mar 8, 2023

    @msacadem

    Without the ability to filter the results, this "feature" is useless on my site as a portion of my site is content aggregation (a custom post type) so whenever I try to link to my own content, it pretty much never comes up in the auto suggest... I have to manually insert the URL of the page. So, being able to filter the post type would be a game changer.

    Yes, very true. I don't know how more complex sites (with many post types) deal with this. Searching for a link is a hassle currently. We have many post types that should not be linkable but they all show up when trying to find a post to link to. It's time-consuming and prone to errors.

  6. getdave commented on Mar 8, 2023

    @getdave
    Contributor

    The current implementation in Gutenberg uses JavaScript to query various API endpoints to get the search results. This makes it difficult to customise with PHP. Not ideal as many of you have pointed out and also inefficient.

    Interestingly I've experiment a bit with creating a REST API endpoint which calls _WP_Editors::wp_link_query. We could look into this more if there is sufficient interest on this Issue.

    I appreciate this is frustrating but there are a limited number of contributors to work on things.

  7. michael-schilde commented on Aug 12, 2023

    @michael-schilde

    I agree with the previous comments, that the current solution is just unusable. I have a plugin in use that manages affiliate links and creates a CPT for them, and that a) avoids relevant suggestions to come up, b) is very slow in providing results and c) is even more painful, as it starts searching as soon as you type the first characters.
    So if you want to search for something like "scooter" it will start searching e.g., at "sc" and when you're done typing the entire word, the results for "sc" finally show, but are completely cluttered with irrelevant stuff that happens to contain "sc" somehow. Then you have to remove the last character again, to trigger yet another search.

    So +1 from my side on solving this in a more user-friendly way, and at least adding the ability to filter CPTs from showing in the results.

  8. misaki-web commented on Aug 8, 2024

    @misaki-web

    Indeed, the lack of customization in the list of links suggested by Gutenberg can be problematic, especially for sites with multiple custom post types. I've published a Gist that demonstrates how the REST API's WP_REST_Search_Controller can be customized as a workaround if such customization is urgently needed. You can find the example here: https://gist.github.com/misaki-web/95a3f5f13bf0534d64e18b43602e2139

    Of course, as I specify in the Gist, editing core files should be avoided, and these changes will be overwritten with WordPress updates. This is meant to serve as a demonstration or a temporary solution until a more permanent fix is implemented.

  9. eidolonnight commented on Dec 3, 2024

    @eidolonnight

    Since @getdave mentioned "We could look into this more if there is sufficient interest on this Issue." I wanted to note that gizmo2501 is experiencing issues with linking that could be resolved by the ability to customize results: Linking is prioritising attachments instead of posts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] Link Editing[Type] Enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions