钩子文档

wp_link_query_args

💡 云策文档标注

概述

wp_link_query_args 是一个 WordPress 过滤器,用于在查询前修改链接查询参数,影响插入链接时模态窗口中可链接内容的列表生成。

关键要点

  • 过滤器名称:wp_link_query_args,允许修改传递给 wp_link_query 函数的参数数组。
  • 参数:$query,一个包含 WP_Query 参数的数组,可调整查询条件如 post_type。
  • 用途:主要用于自定义链接查询结果,例如限制或排除特定文章类型。
  • 引入版本:WordPress 3.7.0,由 _WP_Editors::wp_link_query() 使用。

代码示例

// 示例1:仅显示文章和页面
add_filter( 'wp_link_query_args', 'my_wp_link_query_args' );
function my_wp_link_query_args( $query ) {
    if ( !is_admin() ) {
        $query['post_type'] = array( 'post', 'pages' );
    }
    return $query;
}

// 示例2:移除特定文章类型
add_filter( 'wp_link_query_args', 'remove_these_post_types_from_wp_link_query_args' );
function remove_these_post_types_from_wp_link_query_args( $query ) {
    $cpt_to_remove = 'article';
    $key = array_search( $cpt_to_remove, $query['post_type'] );
    if( $key )
        unset( $query['post_type'][$key] );
    return $query;
}

注意事项

  • 过滤器应用于前端和非管理员环境时需谨慎,避免影响后台功能。
  • 参数修改应遵循 WP_Query 规范,确保查询有效。

📄 原文内容

Filters the link query arguments.

Description

Allows modification of the link query arguments before querying.

See also

  • WP_Query: for a full list of arguments

Parameters

$queryarray
An array of WP_Query arguments.

More Information

The “wp_link_query_args” filter is used to filter the array of arguments passed to the wp_link_query function which is responsible for building the list of linkable content in the modal window that displays when you insert a link. wp_link_query_args allows you to alter the query before the list is rendered on the page.

Source

$query = apply_filters( 'wp_link_query_args', $query );

Changelog

Version Description
3.7.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example Migrated from Codex:

    Any allowable WP_Query parameters can be passed to wp_link_query_args. One example is filtering out post types:

    Show only posts and pages

    If you wanted only allow posts and pages to show up in the linked results, you could do something like this. In this example, we’re going to check to make sure we aren’t in the admin, so results would only be filtered on the front end, but admins could still add links to all post types.

    add_filter( 'wp_link_query_args', 'my_wp_link_query_args' ); 
    
    function my_wp_link_query_args( $query ) {
        // check to make sure we are not in the admin
        if ( !is_admin() ) {
            $query['post_type'] = array( 'post', 'pages' ); // show only posts and pages
        }
    
        return $query;
    }

    Remove specific post types from results

    You’d use something like this if you only wanted to remove specific post types from the results.

    add_filter( 'wp_link_query_args', 'remove_these_post_types_from_wp_link_query_args' );
    
    function remove_these_post_types_from_wp_link_query_args( $query ) {
        // this is the post type I want to exclude
        $cpt_to_remove = 'article';
    
        // 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;
    }