admin管理员组

文章数量:1126449

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?

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?

Share Improve this question edited Sep 28, 2013 at 11:40 gmazzap 46.3k6 gold badges95 silver badges147 bronze badges asked Sep 25, 2013 at 10:36 urok93urok93 4,04412 gold badges65 silver badges104 bronze badges 3
  • 1 What is Link Builder? – brasofilo Commented Sep 27, 2013 at 12:49
  • 1 I mean the button used to link stuff in TinyMCE. – urok93 Commented Sep 28, 2013 at 6:42
  • you can use custom permalink plugin to change the url accordingly – Vikas Gautam Commented Oct 1, 2013 at 5:01
Add a comment  | 

2 Answers 2

Reset to default 4 +50

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":

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', 'page' );  // 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'.

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' );

本文标签: tinymceExcluding post type from WordPress link builder