Excluding post type from WordPress link builder

I have created a custom post type with parameter ‘public’ set to true. This is because I have to have it accessible in plugins that help you to build queries for post display, such as the Views (part of Toolset) plugin.

On the other hand, when I create a new post or page and use the link builder I don’t want entries of this post type to show up in the search box there. Is there any way to achieve this aim?

Related posts

2 comments

  1. Versions <= 3.6.1

    It looks like the function wp_link_query() is used to display the internal posts/pages when you press the Or link to existing content in the “link builder”:

    Link builder

    wp_link_query() is defined in /wp-includes/class-wp-editor.php.

    It is queried via the wp_ajax_wp_link_ajax(), defined in the file /wp-admin/includes/ajax-actions.php.

    These functions don’t have any explicit filters to change the query.

    Version 3.7 (?)

    It looks like there are patches on the way introducing the filter wp_link_query_args

    You can check it out here:

    http://core.trac.wordpress.org/ticket/18042

    The milestone is set as 3.7, so this might be your lucky version 😉

    If that’s the case, then you might probably fix this with:

    Example 1:

    If you want to list the custom post types by hand:

    /**
     * Filter the link query arguments to change the post types. 
     *
     * @param array $query An array of WP_Query arguments. 
     * @return array $query
     */
    function my_custom_link_query( $query ){
    
        // change the post types by hand:
        $query['post_type'] = array( 'post', 'pages' );  // Edit this to your needs
    
        return $query; 
    }
    
    add_filter( 'wp_link_query_args', 'my_custom_link_query' );
    

    Example 2:

    If you only want to eliminate a single custom post type, but keep all the others, then you can try:

    /**
     * Filter the link query arguments to change the post types. 
     *
     * @param array $query An array of WP_Query arguments. 
     * @return array $query
     */
    function my_custom_link_query( $query ){
    
        // custom post type slug to be removed
        $cpt_to_remove = 'news';      // Edit this to your needs
    
        // find the corresponding array key
        $key = array_search( $cpt_to_remove, $query['post_type'] ); 
    
        // remove the array item
        if( $key )
            unset( $query['post_type'][$key] );
    
        return $query; 
    }
    
    add_filter( 'wp_link_query_args', 'my_custom_link_query' );
    

    where I used the demo slug 'news'.

  2. And here’s how you would remove an array of post types from the edit link dialog:

    function removeCustomPostTypesFromTinyMCELinkBuilder($query){
        $key = false;
    
        $cpt_to_remove = array(
            'cpt_one',
            'cpt_two',
            'cpt_three'
        );
    
        foreach ($cpt_to_remove as $custom_post_type) {
            $key = array_search($custom_post_type, $query['post_type']);
            if($key){
                unset($query['post_type'][$key]);
            } 
        }
        return $query; 
    }
    add_filter( 'wp_link_query_args', 'removeCustomPostTypesFromTinyMCELinkBuilder' );
    

Comments are closed.