Wednesday, 30 September 2020

WP Admin: Include custom post type archive in link search results

I've been searching for examples of this online and through the WP documentation on filters but I can't find a suitable hook, so apologies for posting a question without a good example of what I'm trying to do!

When you add a link to text or to a button in the editor, you can search for the page/post you want to link to. What you can't search for is a post type archive link.

I want to be able to type the name of a post type into the search box (pictured below), and include the post type archive link in the search results. In this example, I have a post type called members that I'd like to link to.

enter image description here

I find the need to do this a lot, and I always end up just typing /post-type-link into the box and leaving it at that, but I don't think this is an elegant solution and is clunky for users.

I have tried to write some code, but I don't believe I have the right hook:

function include_cpt_search( $query ) {

    if ( is_admin() && is_single() ) {
        $query->set( 'post_type', array( 'services' ) );
    }

    return $query;

}
add_filter( 'pre_get_posts', 'include_cpt_search' ); 

Has anyone done this before? Know of a filter or hook I could work with? Anything really!



from WP Admin: Include custom post type archive in link search results

No comments:

Post a Comment